java绘制图形的问题
我现在要设计两个按钮,点击一个按钮在窗体上绘制一个矩形,点击另一个按钮绘制一个圆形。我重写了publicvoidpaint(Graphicsg)方法但是在监听里调用的时候...
我现在要设计两个按钮,点击一个按钮在窗体上绘制一个矩形,点击另一个按钮绘制一个圆形。我重写了public void paint(Graphics g)方法 但是在监听里调用的时候用 Mframe.this.paint(new Graphics()) 他会显示Graphics是抽象类无法调用请问应该怎样调用paint()方法才能完成操作。
展开
3个回答
展开全部
简单的例子:repaint就行,不能new Graphics()
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageDemo {
private JPanel panel;
private boolean isDrawRect = false;// 是否绘制矩形,默认不绘制
public ImageDemo(){
JFrame frame = new JFrame("Test");
frame.setVisible(true);// 显示
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();// 屏幕大小
frame.setBounds((d.width - 800) / 2, (d.height - 600) / 2, 800, 600);// 大小,定位
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭方式
frame.setLayout(null);
JButton b1 = new JButton("绘制矩形");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
isDrawRect = true;
panel.repaint();
}
});
b1.setBounds(2, 2, 150, 30);
JButton b2 = new JButton("绘制圆形");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
isDrawRect = false;
panel.repaint();
}
});
b2.setBounds(200, 2, 150, 30);
setPanel();
panel.setBounds(5, 50, 400, 450);
frame.add(b1);
frame.add(b2);
frame.add(panel);
frame.validate();
}
private void setPanel(){
panel = new JPanel(){
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
if(isDrawRect){
g.drawRect(100, 100, 200, 300);
} else {
g.drawOval(100, 100, 200, 200);
}
}
};
}
public static void main(String[] args) throws IOException {
new ImageDemo();
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageDemo {
private JPanel panel;
private boolean isDrawRect = false;// 是否绘制矩形,默认不绘制
public ImageDemo(){
JFrame frame = new JFrame("Test");
frame.setVisible(true);// 显示
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();// 屏幕大小
frame.setBounds((d.width - 800) / 2, (d.height - 600) / 2, 800, 600);// 大小,定位
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭方式
frame.setLayout(null);
JButton b1 = new JButton("绘制矩形");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
isDrawRect = true;
panel.repaint();
}
});
b1.setBounds(2, 2, 150, 30);
JButton b2 = new JButton("绘制圆形");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
isDrawRect = false;
panel.repaint();
}
});
b2.setBounds(200, 2, 150, 30);
setPanel();
panel.setBounds(5, 50, 400, 450);
frame.add(b1);
frame.add(b2);
frame.add(panel);
frame.validate();
}
private void setPanel(){
panel = new JPanel(){
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
if(isDrawRect){
g.drawRect(100, 100, 200, 300);
} else {
g.drawOval(100, 100, 200, 200);
}
}
};
}
public static void main(String[] args) throws IOException {
new ImageDemo();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
repaint()方法就OK了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Graphics g = this.getGraphics();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询