java绘图代码
界面要求:用图形界面实现。 展开
画笔定义
import java.awt.*;
/**
* @author Hardneedl
*/
interface Brush {
void doPaint(Graphics g);
}
画笔工厂
import java.awt.*;
/**
* @author Hardneedl
*/
class BrushFactory {
static final int LINE_BRUSH = 0;
static final int ELLIPSE_BBRUSH = 1;
static final int RECTANGLE_BRUSH = 2;
static final int POLYGON_BRUSH = 3;
static final int ELLIPSE_FILL_BRUSH = 4;
static final int RECTANGLE_FILL_BRUSH = 5;
static final Brush NO = new NONE();
static final Brush LINE = new LineBrush();
static final Brush ELLIPSE = new EllipseBrush();
static final Brush ELLIPSE_FILL = new EllipseFillBrush();
static final Brush RECTANGLE = new RectangleBrush();
static final Brush RECTANGLE_FILL = new RectangleFillBrush();
static final Brush POLYGON = new PolygonBrush();
Brush getBrush(int brushIndex){
switch(brushIndex){
case LINE_BRUSH:return LINE;
case ELLIPSE_BBRUSH:return ELLIPSE;
case RECTANGLE_BRUSH:return RECTANGLE;
case RECTANGLE_FILL_BRUSH:return RECTANGLE_FILL;
case POLYGON_BRUSH:return POLYGON;
case ELLIPSE_FILL_BRUSH:return ELLIPSE_FILL;
default:return NO;
}
}
private static final class LineBrush implements Brush {
public void doPaint(Graphics g) {
Graphics gg =g.create();
gg.setColor(Color.BLACK);
gg.drawLine(70,70, 200,200);
gg.dispose();
}
}
private static final class EllipseBrush implements Brush {
public void doPaint(Graphics g) {
Graphics gg =g.create();
gg.setColor(Color.PINK);
gg.drawOval(100,100,200,50);
gg.dispose();
}
}
private static final class EllipseFillBrush implements Brush {
public void doPaint(Graphics g) {
Graphics gg =g.create();
gg.setColor(Color.PINK);
gg.fillOval(100,100,200,50);
gg.dispose();
}
}
private static final class PolygonBrush implements Brush {
public void doPaint(Graphics g) {
Graphics gg =g.create();
gg.setColor(Color.RED);
gg.drawPolygon(new int[]{48,50,244,483,310}, new int[]{36,192,281,302,77}, 5);
gg.dispose();
}
}
private static final class RectangleBrush implements Brush {
public void doPaint(Graphics g) {
Graphics gg =g.create();
gg.setColor(Color.BLUE);
gg.drawRect(70,70,100,100);
gg.dispose();
}
}
private static final class RectangleFillBrush implements Brush {
public void doPaint(Graphics g) {
Graphics gg =g.create();
gg.setColor(Color.BLUE);
gg.fillRect(70,70,100,100);
gg.dispose();
}
}
private static final class NONE implements Brush {
public void doPaint(Graphics g) {
Graphics gg =g.create();
gg.setColor(Color.RED);
gg.drawString("No brush selected!",20,30);
gg.dispose();
}
}
}
图形界面
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
/**
* @author Hardneedl
*/
final class Draw extends JFrame {
public String getTitle() {return "frame title";}
static private final Dimension size = new Dimension(600, 400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize() {return size;}
private static final Action lineAction = new LineAction();
private static final Action rectangleAction=new RectangleAction();
private static final Action rectangleFillAction=new RectangleFillAction();
private static final Action ellipseAction=new EllipseAction();
private static final Action ellipseFillAction=new EllipseFillAction();
private static final Action polygonAction = new PolygonAction();
private static final BrushFactory brushFactory = new BrushFactory();
private static Brush brush;
private final static JComponent canvas = new JComponent(){
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (brush!=null){
brush.doPaint(g);
}
}
public Border getBorder() {return BorderFactory.createLineBorder(Color.BLACK,2);}
};
Draw() throws HeadlessException {
init();
attachListeners();
doLay();
}
private void init() {
JMenuBar menuBar = new JMenuBar();
menuBar.add(new JMenu(lineAction));
JMenu elp = new JMenu("椭圆");
elp.add(ellipseAction);
elp.add(ellipseFillAction);
menuBar.add(elp);
JMenu rct = new JMenu("矩形");
rct.add(rectangleAction);
rct.add(rectangleFillAction);
menuBar.add(rct);
menuBar.add(new JMenu(polygonAction));
setJMenuBar(menuBar);
}
private void attachListeners() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay() {
Container container = getContentPane();
container.add(canvas,BorderLayout.CENTER);
JPanel buttonsPane = new JPanel(new GridLayout(2,3));
buttonsPane.add(new JButton(lineAction));
buttonsPane.add(new JButton(ellipseAction));
buttonsPane.add(new JButton(rectangleAction));
buttonsPane.add(new JButton(polygonAction));
buttonsPane.add(Box.createHorizontalBox());
buttonsPane.add(new JButton(ellipseFillAction));
buttonsPane.add(new JButton(rectangleFillAction));
container.add(buttonsPane,BorderLayout.SOUTH);
pack();
setVisible(true);
}
private static final class RectangleAction extends AbstractAction {
private RectangleAction() {super("空心矩形");}
public void actionPerformed(ActionEvent e) {
brush = brushFactory.getBrush(BrushFactory.RECTANGLE_BRUSH);
canvas.repaint();
}
}
private static final class RectangleFillAction extends AbstractAction {
private RectangleFillAction() {super("实心矩形");}
public void actionPerformed(ActionEvent e) {
brush = brushFactory.getBrush(BrushFactory.RECTANGLE_FILL_BRUSH);
canvas.repaint();
}
}
private static final class EllipseFillAction extends AbstractAction {
private EllipseFillAction() {super("实心椭圆");}
public void actionPerformed(ActionEvent e) {
brush = brushFactory.getBrush(BrushFactory.ELLIPSE_FILL_BRUSH);
canvas.repaint();
}
}
private static final class EllipseAction extends AbstractAction {
private EllipseAction() {super("空心椭圆");}
public void actionPerformed(ActionEvent e) {
brush = brushFactory.getBrush(BrushFactory.ELLIPSE_BBRUSH);
canvas.repaint();
}
}
private static final class PolygonAction extends AbstractAction {
private PolygonAction() {super("多边形");}
public void actionPerformed(ActionEvent e) {
brush = brushFactory.getBrush(BrushFactory.POLYGON_BRUSH);
canvas.repaint();
}
}
private static final class LineAction extends AbstractAction {
private LineAction() {super("直线");}
public void actionPerformed(ActionEvent e) {
brush = brushFactory.getBrush(BrushFactory.LINE_BRUSH);
canvas.repaint();
}
}
public static void main(String[] args) {new Draw();}
}