import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class DrawingExample implements ActionListener {
JFrame frame;
MyButton button;
MyPanel panel;
int tag = 1; public static void main(String args[]) {
DrawingExample de = new DrawingExample();
de.go();
} public void go() {
frame = new JFrame("Drawing Example");
button = new MyButton("Draw");
button.addActionListener(this);
frame.getContentPane().add(button,"South"); panel = new MyPanel();
frame.getContentPane().add(panel,"Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(360,200);
frame.setVisible(true);
} public void actionPerformed(ActionEvent e) {
//按钮上的文本在Draw与Clear间切换
if (tag==0) {
tag = 1;
button.setText("Draw");
}else {
tag = 0;
button.setText("Clear");
}
panel.repaint();//重绘panel
} //自定义的button
class MyButton extends JButton {
MyButton(String text) {
super(text);
} protected void paintComponent(Graphics g){ //什么时候被调用?被谁调用?
super.paintComponent(g);
g.setColor(Color.red);
int width = getWidth();
int height = getHeight();
g.drawOval(4, 4, width-8, height-8);//绘制椭圆
}
} //自定义的panel
class MyPanel extends JPanel {
protected void paintComponent(Graphics g){ //什么时候被调用?被谁调用?
super.paintComponent(g);
if (tag==0) {
g.setColor(Color.blue); //设置颜色
g.drawLine(40,25,30,50); //绘制直线
g.setColor(Color.green);
g.drawRect(100,50,100,46);//绘制矩形
g.setColor(Color.red);
g.drawRoundRect(73,32,56,37,10,16); //绘制圆角矩形
g.setColor(Color.yellow);
g.fillOval(180,60,60,45); //绘制填充椭圆
g.setColor(Color.pink);
g.fillArc(250,32,90,60,15,30); //绘制填充圆弧
}
}
}
}
类方法重写方法调用java
import java.awt.event.*;
import javax.swing.*;class DrawingExample implements ActionListener {
JFrame frame;
MyButton button;
MyPanel panel;
int tag = 1; public static void main(String args[]) {
DrawingExample de = new DrawingExample();
de.go();
} public void go() {
frame = new JFrame("Drawing Example");
button = new MyButton("Draw");
button.addActionListener(this);
frame.getContentPane().add(button,"South"); panel = new MyPanel();
frame.getContentPane().add(panel,"Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(360,200);
frame.setVisible(true);
} public void actionPerformed(ActionEvent e) {
//按钮上的文本在Draw与Clear间切换
if (tag==0) {
tag = 1;
button.setText("Draw");
}else {
tag = 0;
button.setText("Clear");
}
panel.repaint();//重绘panel
} //自定义的button
class MyButton extends JButton {
MyButton(String text) {
super(text);
} protected void paintComponent(Graphics g){ //什么时候被调用?被谁调用?
super.paintComponent(g);
g.setColor(Color.red);
int width = getWidth();
int height = getHeight();
g.drawOval(4, 4, width-8, height-8);//绘制椭圆
}
} //自定义的panel
class MyPanel extends JPanel {
protected void paintComponent(Graphics g){ //什么时候被调用?被谁调用?
super.paintComponent(g);
if (tag==0) {
g.setColor(Color.blue); //设置颜色
g.drawLine(40,25,30,50); //绘制直线
g.setColor(Color.green);
g.drawRect(100,50,100,46);//绘制矩形
g.setColor(Color.red);
g.drawRoundRect(73,32,56,37,10,16); //绘制圆角矩形
g.setColor(Color.yellow);
g.fillOval(180,60,60,45); //绘制填充椭圆
g.setColor(Color.pink);
g.fillArc(250,32,90,60,15,30); //绘制填充圆弧
}
}
}
}
类方法重写方法调用java
解决方案 »
- 用java如何去掉HTML标签里卖弄的属性
- 如何编译没有main的java
- 多线程之间同步传递数据! 怎么 notify()不能启动另外的线程呢?
- 求一个结果
- 如果IE不能正常地打开Applet ,那么该怎么办?
- 新手求助!关于浮点数在内存中的表示方式
- 在applet在另一台计算机上的浏览器中运行时不能与在本机运行的java应用程序建立socket通信,为什么??
- 简单的问题
- 关于线程调度
- 可以下载jre,jb5_key,jbwheel,windows commander等java常用软件的地方
- 要做一个项目你应该懂得多少?
- 求Struts2+Spring2.5+Hibernate3.3 的 整合jar 包下载地址
paintComponent是一个被保护的方法,因为API上建议不直接重写paint方法
在重绘时重量级组件 最好重写update方法
轻量级组件 重写paint方法
因为这里的调用关系会造成绘制的延迟问题 导致你不能马上得到你要的效果 所以我一般不用repaint 而用papintImmediately();可以马上绘制出效果:
分享文章:
http://blog.csdn.net/w5167839/article/details/9109955