問題:無法畫出圖形
代碼
package GUI.畫筆print;
import GUI.MyClass.MySystemExit;
import java.awt.*;
public class Demo {
public static void main(String[] args) {
new 畫筆PrintFrame();
}
}
class 畫筆PrintFrame extends Frame {
//構造函數初始化Frame
畫筆PrintFrame(){
this.setLocation(100,100);
this.setSize(500,400);
this.setVisible(true);
new MySystemExit(this);
}
//重寫Override print畫筆的方法
@Override
public void print(Graphics g) {
super.print(g);// 此處如果沒有初始化的用處就可以刪去
//畫一個圓
g.drawOval(0,0,30,30);
//填充一個圓形
g.fillOval(40,0,30,30);
//填充一個矩形
g.fillRect(80,0,40,30);
//改變顏色
g.setColor(new Color(99, 255, 240));
//畫一個arc 弧形
g.drawArc(50,0,50,20,40,60);
}
}
二、解決方案:畫筆話到Jpanel裏面,然後將面板添加到 Frame裏
package GUI.AWT.畫筆print;
import GUI.AWT.MyClass.MySystemExit;
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame {
public Demo() throws HeadlessException {
this.setLocation(100, 100);
this.setSize(500, 400);
this.setVisible(true);
new MySystemExit(this);
this.add(new 畫筆PrintFrame());
}
public static void main(String[] args) {
new Demo();
}
}
class 畫筆PrintFrame extends JPanel {
//構造函數初始化Frame
//重寫Override print畫筆的方法
@Override
public void paint(Graphics g) {
super.paint(g);// 此處如果沒有初始化的用處就可以刪去
//畫一個圓
g.drawOval(0, 0, 30, 30);
//填充一個圓形
g.fillOval(40, 0, 30, 30);
//填充一個矩形
g.fillRect(80, 0, 40, 30);
//改變顏色
g.setColor(new Color(99, 255, 240));
//畫一個arc 弧形
g.drawArc(50, 0, 50, 20, 40, 60);
}
}
效果
![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200421160347255.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2phcnZhbjU=,size_16,color_FFFFFF,t_70)