我想在Java中实现的是能够从另一种方法绘制随机形状,而不必从一开始就绘制这些形状(用户自己选择x, y, w, h)
到目前为止我写的是:
public class Design extends JComponent {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
super.paintComponent(g);
}
public void drawRect(int xPos, int yPos, int width, int height) {
Graphics g = null;
g.drawRect(width, height, xPos, yPos);
}
}
正如你在上面看到的,我有drawRect函数编码,但不明白如何使它,所以当我调用drawRect()函数,它得到油漆()函数画一个矩形,而不是什么油漆函数所做的,在它里面,你键入g.p drawRect()和你的x,y,w,h。我这样做的原因,而不是直接使用油漆()是因为我试图使一个组件,而不是每次都要键入油漆()函数,我只是把这个类添加到我的Swing,它完成了。
希望你能理解我想要达到的目的。谢谢。
你需要做的第一件事就是放弃你可以控制绘制过程的想法,你不能,你可以响应绘制事件并向RepaintManager
发出可能需要更新的请求。
基本上,你不直接调用paint
, paint被调用以响应RepaintManager
想要绘制的东西。
看看在AWT和Swing中绘画和执行自定义绘画。
到你的代码。不要覆盖paint
,当然也不要绕过油漆过程,相反,它应该看起来更像这样…
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawRect(g, 10, 10, 10, 10);
}
protected void drawRect(Graphics g, int xPos, int yPos, int width, int height) {
g.drawRect(xPos, yPos, width, height);
}
Graphics#drawRect
实际上是按顺序接受x
、y
、width
、height
参数的…
为什么我这样做,而不是直接使用油漆()的原因是因为我试图使一个组件,而不是必须键入油漆()函数每次
没有意义,这是paint
方法的要点,所以你可以自定义组件的绘画方式,使它的许多实例,并将其添加到你的GUI…这就是所有Swing控件的工作方式…
此外,您可能会发现感兴趣的2D图形
根据注释更新所以,你想要一个其他对象可以调用的方法,它将使他们能够添加一个"矩形"到现有的对象/组件…
首先定义Rectangle
的实例变量
private Rectangle rectangle;
在paintComponent
中,检查rectangle
是否为null
,如果不是,将其涂上…
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (rectangle != null) {
Graphics2D g2d = (Graphics2D)g;
g2d.draw(rectangle);
}
}
现在,更新你的drawRect
方法来创建一个实例Rectangle
,并请求组件被重新绘制
public void drawRect(int xPos, int yPos, int width, int height) {
rectangle = new Rectangle(xPos, yPos, width, height);
repaint();
}
例如…public class Design extends JComponent {
private Rectangle rectangle;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (rectangle != null) {
Graphics2D g2d = (Graphics2D)g;
g2d.draw(rectangle);
}
}
public void drawRect(int xPos, int yPos, int width, int height) {
rectangle = new Rectangle(xPos, yPos, width, height);
repaint();
}
}
现在,如果您想支持拥有多个矩形,您可以简单地使用List
并添加任意数量的Rectangle
实例,在paintComponent
方法
List
。您需要在paintComponent()
方法中调用super.paintComponent()
来完成所有绘画。
我建议您将所有需要的形状存储在List
中,并在paintComponent()
中绘制所有形状。代替drawRect
,使用addRect
之类的东西,为List
添加新的形状,并调用repaint()
方法。检查下一个简单示例:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class TestFrame extends JFrame {
public TestFrame() {
System.out.println("as".equalsIgnoreCase(null));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
pack();
setVisible(true);
}
private void init() {
final Design d = new Design();
d.addRect(0,0,10,20);
JButton b = new JButton("add");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random r = new Random();
int w = r.nextInt(100);
int h = r.nextInt(100);
d.addRect(0,0,w,h);
}
});
add(d);
add(b,BorderLayout.SOUTH);
}
public static void main(String... strings) {
new TestFrame();
}
private class Design extends JComponent {
private static final long serialVersionUID = 1L;
private List<Shape> shapes = new ArrayList<Shape>();
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for(Shape s : shapes){
g2d.draw(s);
}
}
public void addRect(int xPos, int yPos, int width, int height) {
shapes.add(new Rectangle(xPos,yPos,width,height));
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100,100);
}
}
}