import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Administrator
*/
public class hua extends JFrame implements ActionListener{
static hua frame=new hua();
static Button button1=new Button("circle");
static Button button2=new Button("ellipse");
static JPanel pane=new JPanel();
int circle=0;
public static void main(String[] args) {
frame.setTitle("简单的画图程序");
frame.setSize(300, 250);
frame.setLayout(new BorderLayout());
frame.add(pane, BorderLayout.SOUTH);
//frame.setBackground(Color.WHITE);
pane.add(button1);
pane.add(button2);
button1.addActionListener(frame);
button2.addActionListener(frame);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
Button button=(Button)e.getSource();
if(button==button1){
circle=1;
}
else
circle=2;
Graphics g=getGraphics();
paint(g);
}
@Override
public void paint(Graphics g){
g.setFont(new Font("黑体",Font.ITALIC,20));
g.setColor(Color.red);
g.drawString("画圆或画椭圆", 120, 50);
if(circle==1){
/*g.setColor(Color.WHITE);
g.drawOval(80, 60, 70, 120);
g.setColor(Color.red);*/
g.drawOval(100, 90, 70, 70);
repaint();//此处repaint()怎么不起作用呢
}
else if(circle==2) {
/*g.setColor(Color.WHITE);
g.drawOval(100, 90, 70, 70);
g.setColor(Color.red);*/
g.drawOval(80, 60, 70, 120);
repaint();//此处repaint()怎么不起作用呢
} }
}
想用repaint()实现,点画圆就画圆,然后点椭圆,然后画椭圆,圆消失,但是好像实现不了,不知为什么,还请各位帮忙解答一下 1.请告诉我一下为什么不能实现。2.怎么实现? 谢谢
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Administrator
*/
public class hua extends JFrame implements ActionListener{
static hua frame=new hua();
static Button button1=new Button("circle");
static Button button2=new Button("ellipse");
static JPanel pane=new JPanel();
int circle=0;
public static void main(String[] args) {
frame.setTitle("简单的画图程序");
frame.setSize(300, 250);
frame.setLayout(new BorderLayout());
frame.add(pane, BorderLayout.SOUTH);
//frame.setBackground(Color.WHITE);
pane.add(button1);
pane.add(button2);
button1.addActionListener(frame);
button2.addActionListener(frame);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
Button button=(Button)e.getSource();
if(button==button1){
circle=1;
}
else
circle=2;
Graphics g=getGraphics();
paint(g);
}
@Override
public void paint(Graphics g){
g.setFont(new Font("黑体",Font.ITALIC,20));
g.setColor(Color.red);
g.drawString("画圆或画椭圆", 120, 50);
if(circle==1){
/*g.setColor(Color.WHITE);
g.drawOval(80, 60, 70, 120);
g.setColor(Color.red);*/
g.drawOval(100, 90, 70, 70);
repaint();//此处repaint()怎么不起作用呢
}
else if(circle==2) {
/*g.setColor(Color.WHITE);
g.drawOval(100, 90, 70, 70);
g.setColor(Color.red);*/
g.drawOval(80, 60, 70, 120);
repaint();//此处repaint()怎么不起作用呢
} }
}
想用repaint()实现,点画圆就画圆,然后点椭圆,然后画椭圆,圆消失,但是好像实现不了,不知为什么,还请各位帮忙解答一下 1.请告诉我一下为什么不能实现。2.怎么实现? 谢谢
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Administrator
*/
public class hua extends JFrame implements ActionListener{
static hua frame=new hua();
static Button button1=new Button("circle");
static Button button2=new Button("ellipse");
static JPanel pane=new JPanel();
int circle=0;
public static void main(String[] args) {
frame.setTitle("简单的画图程序");
frame.setSize(300, 250);
frame.setLayout(new BorderLayout());
frame.add(pane, BorderLayout.SOUTH);
//frame.setBackground(Color.WHITE);
pane.add(button1);
pane.add(button2);
button1.addActionListener(frame);
button2.addActionListener(frame);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
Button button=(Button)e.getSource();
if(button==button1){
circle=1;
}
else
circle=2;
repaint();
}
@Override
public void paint(Graphics g){
super.paint(g);
g.setFont(new Font("黑体",Font.ITALIC,20));
g.setColor(Color.red);
g.drawString("画圆或画椭圆", 120, 50);
if(circle==1){
/*g.setColor(Color.WHITE);
g.drawOval(80, 60, 70, 120);
g.setColor(Color.red);*/
g.drawOval(100, 90, 70, 70);
}
else if(circle==2) {
/*g.setColor(Color.WHITE);
g.drawOval(100, 90, 70, 70);
g.setColor(Color.red);*/
g.drawOval(80, 60, 70, 120);
} }
}
Button button=(Button)e.getSource();
if(button==button1){
circle=1;
}
else
circle=2;
/*Graphics g=getGraphics();
paint(g);*/
repaint();
}public void paint(Graphics g){
g.setFont(new Font("黑体",Font.ITALIC,20));
g.setColor(Color.red);
g.drawString("画圆或画椭圆", 120, 50);
if(circle==1){
/*g.setColor(Color.WHITE);
g.drawOval(80, 60, 70, 120);
g.setColor(Color.red);*/
g.drawOval(100, 90, 70, 70);
//repaint();//repaint call paint autoly
}
else if(circle==2) {
/*g.setColor(Color.WHITE);
g.drawOval(100, 90, 70, 70);
g.setColor(Color.red);*/
g.drawOval(80, 60, 70, 120);
//repaint();//repaint call paint autoly
} }
repaint引起paint