当前位置: 代码迷 >> J2SE >> 大神关于用Graphics类画图的有关问题
  详细解决方案

大神关于用Graphics类画图的有关问题

热度:7224   发布时间:2013-02-25 00:00:00.0
请教各位大神关于用Graphics类画图的问题
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Graphics;

public class ShowGeometryTest implements ActionListener{
JRadioButton[] radiobtn={new JRadioButton("矩形"),new JRadioButton("有色矩形"),new JRadioButton("椭圆"),new JRadioButton("直线"),new JRadioButton("圆弧"),new JRadioButton("多边形")};
ButtonGroup btngroup=new ButtonGroup();
JPanel btnpane=new JPanel();
DrawPanel drawpane;

public ShowGeometryTest(){
JFrame frame=new JFrame("thlcly");
drawpane=new DrawPanel();
btnpane.setLayout(new GridLayout(3,3));
for(int i=0;i<radiobtn.length;i++){
btngroup.add(radiobtn[i]);
btnpane.add(radiobtn[i]);
radiobtn[i].addActionListener(this);
}
drawpane.setBackground(Color.white);
frame.add(drawpane,BorderLayout.CENTER);
frame.add(btnpane,BorderLayout.SOUTH);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}


class DrawPanel extends JPanel{
public DrawPanel(){}
int x;
int y;
String s="";
public DrawPanel(String str){
x=this.getWidth();
y=this.getHeight();
s=str;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(s.equals("直线")){
g.drawLine(x+20,y+20,x+40,y+40);
}
if(s.equals("矩形")){
System.out.println("dfd");
g.drawRect(x/2-10,y/2-10,x/2+5,y/2+5);
}
if(s.equals("有色矩形")){
g.draw3DRect(x/2-10,y/2-10,x/2+5,y/2+5,true);
}
if(s.equals("椭圆")){
g.drawOval(x/2-10,y/2-10,x/2+5,y/2+5);
}
if(s.equals("圆弧")){
g.drawArc(x/2-10,y/2-10,x/2+5,y/2+5,30, 180);
}
if(s.equals("多边形")){
g.drawPolygon(new int[]{10,20,30,40,50},new int[]{10,20,30,40,50},5);
}
}

}
public void actionPerformed(ActionEvent e){
JRadioButton btn=(JRadioButton)e.getSource();
if(btn==radiobtn[0]){
drawpane=new DrawPanel("矩形");
drawpane.repaint();
}
if(btn==radiobtn[1]){

drawpane=new DrawPanel("有色矩形");
drawpane.repaint();
}

if(btn==radiobtn[2]){

drawpane=new DrawPanel("椭圆");
drawpane.repaint();
}

if(btn==radiobtn[3]){
drawpane=new DrawPanel("直线");
drawpane.repaint();
}
if(btn==radiobtn[4]){
drawpane=new DrawPanel("圆弧");
drawpane.repaint();
}
if(btn==radiobtn[5]){
drawpane=new DrawPanel("多边形");
drawpane.repaint();
}
}
public static void main(String []args){
new ShowGeometryTest();
}
}
为什么上边这个程序中调用repaint()方法后系统不会调用paintComponent(Graphics g)方法,而且画板上不会显示图像?跪求各位大神赐教..............

------解决方案--------------------------------------------------------
Java code
public void actionPerformed(ActionEvent e) {  // ...    if(btn==radiobtn[0]){    drawpane=new DrawPanel("矩形");    drawpane.repaint();  }  // ...}
------解决方案--------------------------------------------------------
drawpane=new DrawPanel("矩形");
应该是new出来的没添加的容器里
------解决方案--------------------------------------------------------
探讨

1楼的大神经过你的知道我的问题解决了,但是能否说的更清楚一点,就是我的程序那样写为什么不行?调用了repaint()方法后为什么不能强制系统调用paintComponent(Graphics g)?什么情况下系统会调用paintComponent(Graphics g)方法?
  相关解决方案