java awt唯一的repaint(),update(),paint()的问题
这是具有给定要求的练习。
- 仅awt,因此没有FX或Swing
- 2类首先是绘制2个按钮的主,然后将
- 随机绘制一个矩形或圆形绘制,如果您按Draw Button,则
- 用帆布部分按下另一个类,我们将对象
- 从对象中清除了 画布如果您按“清除”按钮,
- 两个类都需要扩展框架,
我大部分都解决了它,但是我现在有一个问题 清除按钮也可以工作绘制按钮,但问题是我不能接一个地绘制多个对象。 如果我绘制一个对象,然后另一个对象,则第一个由于repaint()方法而消失。 我的两个想法是Override Update()或将阵列列表中的图形安全性,或者
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();
}
我希望问题以某种方式可以理解。因为很难找到解决方案或想法,这很难。
It's an exercise with given requirements.
- awt only, so no fx or swing
- 2 classes first is the Main with 2 buttons draw and clear
- 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.
My 2 ideas are override update() or safe the graphics in an arrayList or something
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您绘制的每种形状保持在某种
list
中,这样,您可以继续将形状添加到列表中,然后继续绘制列表中的内容。nb:我知道,它不使用多个
frame
s,那是因为这不是一个好主意做一些工作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.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 ????