简体   繁体   English

java awt 只有 repaint(),update(),paint() 的问题

[英]java awt only question for repaint(),update(),paint()

It's an exercise with given requirements.这是一个有给定要求的练习。

  • awt only, so no fx or swing仅 awt,所以没有 fx 或 swing
  • 2 classes first is the Main with 2 buttons draw and clear 2类首先是带有2个按钮绘制和清除的主要
  • draw random a rectangle or circle if you press the draw button如果按下绘图按钮,则随机绘制一个矩形或圆形
  • the other class with the canvas part where we paint the objects另一个带有画布部分的类,我们在其中绘制对象
  • clear the canvas from the objects if you press the clear button如果按下清除按钮,则从对象中清除画布
  • both classes need to extend frame两个类都需要扩展框架

I solved it for the most part but I have a question right now the clear button works the draw buttons work too but the problem is I can't draw multiple object one after another.我解决了大部分问题,但我现在有一个问题,清除按钮有效,绘制按钮也有效,但问题是我不能一个接一个地绘制多个对象。 If I draw an object and then another the first disappears because of the repaint() method.如果我绘制一个对象,然后另一个对象由于 repaint() 方法而消失。 My 2 ideas are override update() or safe the graphics in an arrayList or something我的 2 个想法是覆盖 update() 或保护 arrayList 中的图形或其他东西

public class Main extends Frame implements ActionListener {
CanvasPart c;

public static void main(String[] args) {
    new Main();

}

public Main() {
    super();
    setSize and Title....

    Button draw = new Button("draw");
    Button clear = new Button("clear");
    draw.addActionListener(this);
    clear.addActionListener(this);

    **new Panel and adding buttons**
    c = new CanvasPart();


}//Main

@Override
public void actionPerformed(ActionEvent act) {
    try {
        if(act.getActionCommand().equals("clear")) {
            c.clear();
        }

        if(act.getActionCommand().equals("draw")) {
            c.repaint();
        }
    }catch(Exception exep) {
        exep.printStackTrace();
    }

}

public class CanvasPart extends Frame{
Canvas canvas = new Canvas();
public DrawFrame() {
    **set size location and name ....**
}

@Override
public void paint(Graphics gra) {
    super.paint(gra);
    Graphics2D gra2D = (Graphics2D) gra;
**pick random rectangle or circle......
and paint them on canvas **
}


    public void clear(){
    repaint();
}

I hope the problem is somehow understandable.我希望这个问题是可以理解的。 Because it's awt only it's way harder to find solutions or ideas.因为它只是很难找到解决方案或想法。

Keep each shape you draw in some kind of List , this way, you can keep adding shapes to the list and just keep on painting the contents of the list.将您绘制的每个形状保留在某种List中,这样,您可以继续将形状添加到列表中,并继续绘制列表的内容。

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public final class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Frame frame = new Frame();
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends Panel {

        private DrawingPane drawingPane;

        public MainPane() {
            setLayout(new BorderLayout());
            Button rectangleButton = new Button("Rectangle");
            Button circleButton = new Button("Circle");
            Button clearButton = new Button("Clear");
            Panel buttonPane = new Panel(new GridBagLayout());
            buttonPane.add(rectangleButton);
            buttonPane.add(circleButton);
            buttonPane.add(clearButton);

            rectangleButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    drawingPane.addRectangle();
                }
            });
            circleButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    drawingPane.addCircle();
                }
            });
            clearButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    drawingPane.clear();
                }
            });

            add(buttonPane, BorderLayout.NORTH);

            drawingPane = new DrawingPane();
            add(drawingPane);
        }

    }

    public class DrawingPane extends Panel {

        private List<Shape> shapes = new ArrayList<>();
        private Random rnd = new Random();

        public DrawingPane() {
        }

        public void addRectangle() {
            int width = rnd.nextInt(getWidth() / 2) + 5;
            int height = rnd.nextInt(getWidth() / 2) + 5;
            int x = rnd.nextInt(getWidth() - width);
            int y = rnd.nextInt(getHeight() - height);
            shapes.add(new Rectangle(x, y, width, height));
            repaint();
        }

        public void addCircle() {
            int width = rnd.nextInt(getWidth() / 2) + 5;
            int height = rnd.nextInt(getWidth() / 2) + 5;
            int x = rnd.nextInt(getWidth() - width);
            int y = rnd.nextInt(getHeight() - height);
            shapes.add(new Ellipse2D.Double(x, y, width, height));
            repaint();
        }

        public void clear() {
            shapes.clear();
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (Shape shape : shapes) {
                g2d.draw(shape);
            }
        }

    }
}

nb: I know, it's not using multiple Frame s, that's because it's not a good idea and I'm a stick in the mud for making you actually do some work 😜 nb:我知道,它没有使用多个Frame ,那是因为这不是一个好主意,而且我是让你真正做一些工作的泥潭😜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM