gpt4 book ai didi

java - 尝试在单击按钮时在 JPanel 中添加动态定位的图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:33 24 4
gpt4 key购买 nike

我正在尝试向现有的 JPanel 添加/绘制单个 Graphics 对象。我正在生成 10 个随机大小和放置在面板中的初始 Graphics 对象,但我想一次添加一个额外的绘制对象,随机大小和放置,就像最初的 10 个一样。

目前,AddNewDrawItem 类未呈现新的 Graphics 对象。

感谢您的输入。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class Painter{

private DrawingPanel dp = new DrawingPanel();

//constructor
public Painter(){
buildGUI();
}

private void buildGUI(){
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setTitle("Paint drawing demonstration");
JPanel headerPanel = new JPanel();
headerPanel.add(new JLabel("The drawing panel is below"));
JButton addNew = new JButton("Add New Graphic");
addNew.addActionListener(new addNewClickHandler());
headerPanel.add(addNew);
frame.add(BorderLayout.NORTH,headerPanel);
frame.add(BorderLayout.SOUTH,this.dp);
frame.pack();
frame.setVisible(true);
}

class DrawingPanel extends JPanel {

public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);

int x, posx, posy, width, height;

for(x=0;x<=10;x++){
//even number differentiation
if(x % 2 == 0){
g.setColor(Color.red);
}else{
g.setColor(Color.blue);
}

Random rand = new Random();
posx = rand.nextInt(300);
posy = rand.nextInt(300);
width = rand.nextInt(40);
height = rand.nextInt(40);

//System.out.println("the ran x pos is: " + posx);
g.fillRect(posx, posy, width, height);
}//end for
}//end paintComponent

public Dimension getPreferredSize() {
return new Dimension(400,400);
}
}// end DrawingPanel

private class addNewClickHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.print("in addNew_click_handler click handler");//trace debug
AddNewDrawItem newItem = new AddNewDrawItem();
newItem.repaint();
System.out.print("after repaint() in addNew_click_handler click handler");//trace debug
}
}

class AddNewDrawItem extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
int posx, posy, width, height;

Random rand = new Random();
posx = rand.nextInt(300);
posy = rand.nextInt(300);
width = rand.nextInt(40);
height = rand.nextInt(40);
g.setColor(Color.cyan);
g.fillRect(posx, posy, width, height);

}//end paintComponent
}//end AddNewDrawItem

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

}//end class Painter

最佳答案

您的代码有一些问题,其中之一是您的 paintComponent 方法中有程序逻辑:代码随机更改此方法中显示的值,这意味着您的显示会在重新绘制时发生变化,不管你想不想。为了解情况,请尝试调整 GUI 的大小,您会在绘制的红色和蓝色矩形中看到一些迷幻的变化。

现在关于你现在的问题,它的解决方案与我上面描述的问题的解决方案类似。我建议...

  • 您创建了一个 ArrayList<Rectangle2D> ,
  • 在类的构造函数中创建随机矩形,这样它们只创建一次,然后将它们放在上面的 ArrayList 中。
  • 您在 JPanel 的 paintComponent 方法中遍历此 ArrayList,边画边画。这样 paintComponent 只做它应该做的事情,
  • 您创建一个 MouseAdapter 派生对象并将其作为 MouseListener 和 MouseMotionListener 添加到您的 DrawingPanel
  • 您使用上面的监听器创建一个新的 Rectangle2D 对象,完成后将其添加到 ArrayList 并在 DrawingPanel 上调用重绘
  • 您通过按钮的 Action 监听器激活了鼠标适配器。

我就到此为止了,但我想您明白了,如果您不明白,请提出您可能有的任何问题。

关于java - 尝试在单击按钮时在 JPanel 中添加动态定位的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7479132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com