Java 尝试通过单击按钮在JPanel中添加动态定位的图像
我正在尝试向现有JPanel添加/绘制单个图形对象。我正在生成10个随机大小和放置在面板中的初始图形对象,但希望添加额外的绘制对象,一次一个,随机大小和放置与初始10个相同 当前,AddNewDrawItem类未呈现新图形对象 谢谢你的意见Java 尝试通过单击按钮在JPanel中添加动态定位的图像,java,swing,graphics2d,Java,Swing,Graphics2d,我正在尝试向现有JPanel添加/绘制单个图形对象。我正在生成10个随机大小和放置在面板中的初始图形对象,但希望添加额外的绘制对象,一次一个,随机大小和放置与初始10个相同 当前,AddNewDrawItem类未呈现新图形对象 谢谢你的意见 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.uti
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
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Random;
公共级画家{
私有DrawingPanel dp=新DrawingPanel();
//建造师
公共画家(){
buildGUI();
}
私有void buildGUI(){
JFrame=新JFrame();
frame.setLayout(新的BorderLayout());
框架。设置标题(“绘制图纸演示”);
JPanel headerPanel=新的JPanel();
headerPanel.add(新的JLabel(“绘图面板在下面”);
JButton addNew=新JButton(“添加新图形”);
addNew.addActionListener(newAddNewClickHandler());
headerPanel.add(addNew);
框架。添加(边界布局。北部,头部面板);
frame.add(BorderLayout.SOUTH,this.dp);
frame.pack();
frame.setVisible(true);
}
类DrawingPanel扩展了JPanel{
公共组件(图形g){
超级组件(g);
这个.背景(颜色.白色);
int x,posx,posy,宽度,高度;
对于(x=0;x您的代码有一些问题,其中之一是您的paintComponent方法中有程序逻辑:代码随机更改此方法中显示的值,这意味着您的显示将在重新绘制时发生更改,无论您是否愿意。要确定这一点,请尝试调整GUI的大小,您将看到一些p绘制的红色和蓝色矩形中的幻觉变化
至于你目前的问题,解决方法与我上面描述的问题类似。我建议
- 创建一个
ArrayList
- 在类的构造函数中创建随机矩形,这样它们只创建一次,然后将它们放在上面的ArrayList中
- 在JPanel的paintComponent方法中迭代这个ArrayList,边画边画。这样,paintComponent只画它应该画的东西
- 创建一个MouseAdapter派生对象,并将其作为MouseListener和MouseMotionListener添加到DrawingPanel
- 使用上面的侦听器创建一个新的矩形2D对象,完成后将其添加到ArrayList并在DrawingPanel上调用repaint
- 通过按钮的动作侦听器激活鼠标适配器
我就到此为止,但我想你明白了,如果你不明白,请问你可能有的任何问题。谢谢,我想我理解气垫船所说的。我会尝试一下更新,并在完成后发布重新编写的代码。