[英]Java Graphics trying to draw rectangle
我的目標是制作一個包含矩形的類,然后使用它並在其他類中對其進行更改。 我試圖編寫這段代碼,並創建一個對象Rect rect = new Rect();
但是,當我啟動程序時,矩形不會顯示。 我也試圖用window.add(rect);
添加它window.add(rect);
但有同樣的問題,我敢肯定我做錯了什么,但我真的不知道該怎么辦。
我嘗試的另一件事是從其他類Rect.drawRect(g);
調用方法Rect.drawRect(g);
但是然后它要求“ Argument”,如果我像在drawRect方法中那樣添加Argument g,它會說“ g無法解析為變量”
我希望有人能解釋並告訴我我做錯了什么,也很高興知道如何制作矩形或圓形,然后在其他課程中使用它,並可能更改其顏色和大小,我只在一堂課
import javax.swing.JFrame;
public class Main extends Rect{
public static void main(String[] args ) {
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();
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Rect extends JPanel{
public void drawRect(Graphics g){
g.setColor(Color.RED);
g.fillRect(100, 100, 200, 200);
}
}
UDP:您需要重寫void paintComponent(Graphics g)
而不是void drawRect(Graphics g)
並在方法內部調用super.paintComponent(g)
。 然后可以使用window.add(rect);
。 感謝@FredK進行更正
最重要的是,您需要編寫一些代碼來進行繪畫。 這是通過重寫Rect類中的paintComponent
方法來完成的,如下所示:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(100, 100, 200, 200);
}
第二個問題是您希望能夠從其他類中更改矩形的顏色和大小。 對於一個簡單的示例,可以通過在Rect
類中添加一些靜態值來輕松完成此操作:
public static Color myColor = Color.RED;
public static int myX = 100;
public static int myY = 100;
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);
}
現在,無論何時何地使用“ Rect
面板,它都會根據靜態值顯示矩形。
例如,下面是一個簡單且有效的程序,請注意它如何使用以下內容:
//create Rect
Rect rect = new Rect();
//set the size of the new panel
rect.setPreferredSize(new Dimension(800, 600));
//add the rect to your JFrame
window.add(rect);
//now you can change the color for all Rect instances
//Note how I use Rect instead of rect, however, both will work.
Rect.myColor = Color.BLUE;
//And dont forget to repaint it if you want to see the changes immediatly
rect.repaint();
完整示例,主類:
import javax.swing.JFrame;
import java.awt.Color;
public class Main{
//Note how we don't need to extend the Rect class (It just adds confusion)
public static void main(String[] args ) {
JFrame window = new JFrame("test");
window.setSize(1000, 800);
window.setVisible(true);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create Rect
Rect rect = new Rect();
//set the size of the new panel
rect.setPreferredSize(new Dimension(800, 600));
//add the rect to your JFrame
window.add(rect);
//now you can change the color for all Rect instances
//Note how I use Rect instead of rect, however both will work.
Rect.myColor = Color.BLUE;
//And dont forget to update it
rect.repaint();
}
}
和Rect類:
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);
}
}
請注意,如果您不想每次更改顏色/大小時都調用Rect.repaint()
則只需創建一個新方法即可更改每個值並包含repaint()
,例如:
public void changeWidth(int width){
myWidth = width;
repaint();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.