All pastes #939159 Raw Edit

JAVA

public java v1 · immutable
·published 2008-03-12 02:15 UTC
rendered paste body
public interface Shape {	public void draw();}/* ---------------------------------------------------------------------------------------- */import java.awt.*;import javax.swing.*;public class DrawPolyArray extends JFrame {	public DrawPolyArray() {		setLayout(new GridLayout(2, 2, 5, 5));		Component[] array = new JPanel[4];		array[0] = new Line();		array[1] = new Rectangle();		array[2] = new RoundRectangle();		array[3] = new Circle();				for(int i = 0; i < array.length; i++) {			add(array[i]);		}			}		public static void main(String[] args) {		/** Creates frame, and then centers, displays and adds other atributes */		DrawPolyArray frame = new DrawPolyArray();		frame.setTitle("DrawPolyArray");		frame.setLocationRelativeTo(null);		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		frame.setSize(400, 200);		frame.setVisible(true);	}	}	/** Creates a line */class Line extends JPanel implements Shape {	public Line() { draw(); }	public void draw() { add(new FigurePanel(FigurePanel.LINE)); }}/** Creates a rectangle */class Rectangle extends JPanel implements Shape {	public Rectangle() { draw(); }	public void draw() { add(new FigurePanel(FigurePanel.RECTANGLE)); }}/** Creates a round rectangle */class RoundRectangle extends JPanel implements Shape  {	public RoundRectangle() { draw(); }	public void draw() { add(new FigurePanel(FigurePanel.ROUND_RECTANGLE)); }}/** Creates a circle */class Circle extends JPanel implements Shape {	public Circle() { draw(); }	public void draw() { add(new FigurePanel(FigurePanel.OVAL)); }}/* ---------------------------------------------------------------------------------------- */import java.awt.*;import javax.swing.*;public class FigurePanel extends JPanel {//	Define constants	public static final int LINE = 1;	public static final int RECTANGLE = 2;	public static final int ROUND_RECTANGLE = 3;	public static final int OVAL = 4;		private int type = 1;	private boolean filled;		/** Construct a default FigurePanel */	public FigurePanel() {	}		/** Construct a FigurePanel with the specified type */	public FigurePanel(int type) {		this.type = type;	}		/** Construct a FigurePanel with the specified type and filled */	public FigurePanel(int type, boolean filled) {		this.type = type;		this.filled = filled;	}		/** Draw a figure on the panel */	public void paintComponent(Graphics g) {		super.paintComponent(g);		int width = getSize().width;		int height = getSize().height;				switch (type) {		case LINE: //Display two cross lines			g.setColor(Color.BLACK);			g.drawLine(10, 10, width - 10, height - 10);			g.drawLine(width - 10, 10, 10, height - 10);			break;		case RECTANGLE: //Display a rectangle			g.setColor(Color.BLUE);			if(filled) {				g.fillRect((int)(0.1 * width), (int)(0.1 * height), 						(int)(0.8 * width), (int)(0.8 * height));			} else 				g.drawRect((int)(0.1 * width), (int)(0.1 * height), 						(int)(0.8 * width), (int)(0.8* height));			break;		case ROUND_RECTANGLE: //Display a round rectangle			g.setColor(Color.RED);			if(filled) {				g.fillRoundRect((int)(0.1 * width), (int)(0.1 * height), 						(int)(0.8 * width), (int)(0.8 * height), 20, 20);			} else 				g.drawRoundRect((int)(0.1 * width), (int)(0.1 * height), 						(int)(0.8 * width), (int)(0.8* height),20, 20);			break;		case OVAL: //Display an oval			g.setColor(Color.BLACK);			if(filled) {				g.fillOval((int)(0.1 * width), (int)(0.1 * height), 						(int)(0.8 * width), (int)(0.8 * height));			} else 				g.drawOval((int)(0.1 * width), (int)(0.1 * height), 						(int)(0.8 * width), (int)(0.8* height));			break;		}	}		/** Set a new figure type */	public void setType(int type) {		this.type = type;		repaint();	}		/** Return a figure type */	public int getType() { return type; }		/** Set a new filled property */	public void setFilled(boolean filled) {		this.filled = filled;		repaint();	}		/** Check if the figure is filled */	public boolean isFilled() { return filled; }		/** Specifiy preferred size */	public Dimension getPreferredSize() { return new Dimension(80, 80); }}