1
因此,我已經做了幾個月的Java,現在我正在學習Graphics。所以,我正在嘗試製作一種繪畫類的東西,在那裏我可以用鼠標畫畫。沒有什麼真正的想法,只是讓我開始的東西。只要我拖動鼠標,該程序就會繪製一個小點。它有些作品,除了它沒有保存我的舊點之外。它只是創建一個新的!那將是怎樣,如果有人可以幫我解決這個問題:Java:程序不保存舊圖形
主要類:
import javax.swing.JFrame;
public class Test{
public static void main(String args[]){
Ploofer ploof = new Ploofer();
PumpkinPie f = new PumpkinPie(ploof);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1000,1000);
f.setResizable(false);
ploof.setSize(1000,1000);
f.add(ploof);
f.setVisible(true);
}
}
「Ploofer」 類:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Ploofer extends JPanel{
PumpkinPie pObj = new PumpkinPie(this);
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
this.setBackground(Color.WHITE);
g2d.setColor(new Color(190, 50, 0));
if(pObj.draw==true){
g2d.fillRect(pObj.x, pObj.y, 2, 2);
pObj.draw = false;
}
}
@Override
public void update(Graphics g){
paint(g);
}
//I tried to override update, but it didn't really help
}
「南瓜派」 類:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Ploofer extends JPanel{
PumpkinPie pObj = new PumpkinPie(this);
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
this.setBackground(Color.WHITE);
g2d.setColor(new Color(190, 50, 0));
/* g2d.fillRect(475, 475, 50, 50);
g2d.drawString("^Red^", 484, 540); */
if(pObj.draw==true){
g2d.fillRect(pObj.x, pObj.y, 2, 2);
pObj.draw = false;
}
}
@Override
public void update(Graphics g){
paint(g);
}
//I tried to override update, but it didn't really help
}
非常感謝!我想我以前有過這樣的事情,但顯然我做錯了一些事情!但是,它現在終於起作用了!再一次,謝謝你! – Zyphicx