Java I';我正在尝试用图形和画一些矩形
您好,我正在尝试学习java中的图形,同时学习类和对象。我现在的目标是制作一个包含不同类的矩形或圆形的程序,然后我想在其他类中使用这些矩形和圆形,并更改它们的参数,如大小、颜色和位置,以绘制某种图案 我现在的问题是我可以做一个矩形,我想我甚至可以做第二个,但是我不能改变它的参数(颜色、大小和位置),我试着在代码的这一部分添加变量Java I';我正在尝试用图形和画一些矩形,java,swing,class,object,graphics,Java,Swing,Class,Object,Graphics,您好,我正在尝试学习java中的图形,同时学习类和对象。我现在的目标是制作一个包含不同类的矩形或圆形的程序,然后我想在其他类中使用这些矩形和圆形,并更改它们的参数,如大小、颜色和位置,以绘制某种图案 我现在的问题是我可以做一个矩形,我想我甚至可以做第二个,但是我不能改变它的参数(颜色、大小和位置),我试着在代码的这一部分添加变量Rect Rect=new Rect(int variables)但它不起作用 通常我可以解决像这样的简单问题,但我真的不明白类和对象在java中是如何工作的,如果有人能
Rect Rect=new Rect(int variables)代码>但它不起作用
通常我可以解决像这样的简单问题,但我真的不明白类和对象在java中是如何工作的,如果有人能给我一些帮助就好了
这是我的密码
public class Main{
public static void main(String[] args ) {
Pattern.drawPattern();
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Rect extends JPanel{
public static Color myColor = Color.RED;
public static int myX = 10;
public static int myY = 10;
public static int myWidth = 200;
public static int myHeight = 200;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(myColor);
g.fillRect(myX, myY, myWidth, myHeight);
}
}
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
public class Pattern {
public static void drawPattern() {
JFrame window = new JFrame("test");
window.setSize(1000, 800);
window.setVisible(true);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rect rect = new Rect();
Rect rect1 = new Rect();
window.add(rect);
window.add(rect1);
Rect.myColor = Color.lightGray;
}
}
这里有很多问题,但我能看到的主要问题是:
- 在Rect类中过度使用静态修饰符。通过使用静态字段,Rect实例将不会有自己的唯一状态、颜色和位置。将所有这些字段设置为私有非静态(实例)。如果这会导致编译问题,请通过不使字段为静态字段,而是不尝试从类中访问字段来解决
- 如果您想在类之外更改这些字段,也可以使用这些字段setter方法。和getter方法,如果要查询它们
- 您忽略了JFrame的contentPane默认使用BorderLayout这一事实。此布局将覆盖先前添加的组件和接下来添加的任何组件。如果在此容器中需要多个组件,请使用不同的布局
- 但您的主要问题是Rect不应该扩展JPanel,它不应该是一个组件类,而应该是一个逻辑类李>
- 相反,创建一个扩展JPanel并执行所有绘图的类,然后给它多个Rect实例以在其单个paintComponent方法中绘制。您可以为此使用
ArrayList
- 将此单个图形JPanel添加到JFrame。如果这样做,那么就不需要更改JFrame的布局管理器,因为BorderLayout可以很好地工作,允许图形JPanel填充JFreme的中心李>
小诡辩:
- 避免给出与常见Java核心类冲突的类名称,例如Java正则表达式分析中经常使用的模式
- 不知道为什么您甚至需要Pattern类,因为它不做任何在main方法中做不到的有用的事情李>
例如,Rect(或此处命名为Rect2以显示它与您的类不同)的外观可能类似:
// imports here
public class Rect2 {
private Color myColor = Color.RED;
private int x = 10;
private int y = x;
private int width = 200;
private int height = width;
public Rect2() {
// default constructor
}
public Rect2(Color myColor, int x, int y, int width, int height) {
this.myColor = myColor;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// method used to allow the rectangle to be drawn
public void draw(Graphics g) {
g.setColor(myColor);
g.fillRect(x, y, width, height);
}
public void setMyColor(Color myColor) {
this.myColor = myColor;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
// more setters and some getters if need be
}
// imports here
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private List<Rect2> rectList = new ArrayList<>();
// ..... more code
public void addRect2(Rect2 rect) {
rectList.add(rect);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// iterate through the rectList and draw all the Rectangles
for (Rect2 rect : rectList) {
rect.draw(g);
}
}
// ...... more code
}
图中的JPanel是这样的:
// imports here
public class Rect2 {
private Color myColor = Color.RED;
private int x = 10;
private int y = x;
private int width = 200;
private int height = width;
public Rect2() {
// default constructor
}
public Rect2(Color myColor, int x, int y, int width, int height) {
this.myColor = myColor;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// method used to allow the rectangle to be drawn
public void draw(Graphics g) {
g.setColor(myColor);
g.fillRect(x, y, width, height);
}
public void setMyColor(Color myColor) {
this.myColor = myColor;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
// more setters and some getters if need be
}
// imports here
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private List<Rect2> rectList = new ArrayList<>();
// ..... more code
public void addRect2(Rect2 rect) {
rectList.add(rect);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// iterate through the rectList and draw all the Rectangles
for (Rect2 rect : rectList) {
rect.draw(g);
}
}
// ...... more code
}
看:嗯,把所有东西都放在IntelliJ的一个类中,效果很好。但是如果手动触发重新绘制,可能会有所帮助。因为如果你改变了你所画内容的属性,你就需要这么做。请看更新来回答。如果有任何问题,请询问