public void paint1(Graphics g)
{
super.paint();//这样可以么?
Rectangle rect=getBounds();
int X=(int)(rect.width/3.0);
int Y=(int)(rect.height/3.0);
for(int x=0;x<rect.width;x=x+X)
for(int y=0;y<rect.height;y=y+Y)
{
g.setColor(Color.red);
g.drawOval(x,y,X,Y);
}
}
{
super.paint();//这样可以么?
Rectangle rect=getBounds();
int X=(int)(rect.width/3.0);
int Y=(int)(rect.height/3.0);
for(int x=0;x<rect.width;x=x+X)
for(int y=0;y<rect.height;y=y+Y)
{
g.setColor(Color.red);
g.drawOval(x,y,X,Y);
}
}
类RectangleandOvalHandler 中的component接收了原来的可以画图的RectangleandOval 的实例的引用
也就是说这个component 可以画图,那么下面的component.paint1(g)和 component.paint2(g);那这两个方法是写在外部类呢,还是写在内部类中呢,也就是说内部类中的component调用的paint1(g)和paint2(g)的方法写在哪个类,而且我好象写在那而都不对。
component.paint1(g);
component.paint2(g);
就可以了,但好象没什么理由,我也搞不清这样怎么就画出来了。
this.paint2(g);就可以了,但好象没什么理由,我也搞不清这样怎么就画出来了
当然要把paint1和paint2方法写在内部类中,我都糊涂了,希望大家帮我。
import javax.swing.*;
import java.awt.event.*;public class RectangleandOval extends JFrame
{
Rectangle rect;
JButton button1,button2,button3;
DrawPanel drawPanel;
public RectangleandOval()
{
super("Drawing Rectangle and Oval");
Container container=getContentPane();
JPanel panel=new JPanel();
container.add(panel,BorderLayout.NORTH);
button1=new JButton("Rectangle");
button2=new JButton("Oval");
button3=new JButton("Clear");
panel.add(button1);
panel.add(button2);
panel.add(button3);
drawPanel = new DrawPanel();
container.add(drawPanel, BorderLayout.CENTER); RectangleandOvalHandler handler=new RectangleandOvalHandler(drawPanel);
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
setSize(300,400);
show();
} private class DrawPanel extends JPanel
{
int flag = 0; protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (flag == 1)
{
paint1(g);
}
else if (flag == 2) {
paint2(g);
}
}
public void paint1(Graphics g)
{
Rectangle rect=getBounds();
int X=(int)(rect.width/3.0);
int Y=(int)(rect.height/3.0);
for(int x=0;x<rect.width;x=x+X)
for(int y=0;y<rect.height;y=y+Y)
{
g.setColor(Color.red);
g.drawOval(x,y,X,Y);
}
}
public void paint2(Graphics g)
{
Rectangle rect=getBounds();
int X=(int)(rect.width/3.0);
int Y=(int)(rect.height/3.0);
for(int x=0;x<rect.width;x=x+X)
for(int y=0;y<rect.height;y=y+Y)
{
g.setColor(Color.green);
g.drawRect(x,y,X,Y);
}
}
}
class RectangleandOvalHandler implements ActionListener
{
DrawPanel drawPanel;
public RectangleandOvalHandler(DrawPanel drawPanel)
{
this.drawPanel=drawPanel;
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
drawPanel.flag = 1;
drawPanel.repaint();
}
if(e.getSource()==button2)
{
drawPanel.flag = 2;
drawPanel.repaint();
}
else if(e.getSource()==button3) {
drawPanel.flag = 0;
drawPanel.repaint();
}
}
}
public static void main(String[] args)
{
RectangleandOval RO=new RectangleandOval();
RO.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.applet.*;
import java.awt.*;public class Button2Applet extends Applet
{
public void init()
{
Button red=new Button("Red");
add(red);
red.addActionListener(new BGSetter(this,Color.red));
Button blue=new Button("Blue");
add(blue);
blue.addActionListener(new BGSetter(this,Color.blue));
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;public class BGSetter extends Object implements ActionListener
{
Component component;
Color color;
public BGSetter(Component component,Color color)
{
this.component=component;
this.color=color;
}
public void actionPerformed(ActionEvent evt)
{
component.setBackground(color);
component.repaint();
}
}
差不多的一个程序怎么就能运行,我的我就不知道错在那而了,]
我是这样理解的,component 接受原来的一个事例的引用,说明他能够画图,setBackground,repaint着两个方法到底是外部类的还是内部类的。
import javax.swing.*;
import java.awt.event.*;public class RectangleandOval extends JFrame
{
Rectangle rect;
Graphics g;
JButton button1,button2,button3;
public RectangleandOval()
{
super("Drawing Rectangle and Oval");
Container container=getContentPane();
JPanel panel=new JPanel();
container.add(panel,BorderLayout.NORTH);
button1=new JButton("Rectangle");
button2=new JButton("Oval");
button3=new JButton("Clear");
panel.add(button1);
panel.add(button2);
panel.add(button3); RectangleandOvalHandler handler=new RectangleandOvalHandler(this);
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
setSize(300,400);
show();
g = getGraphics();
}public void paint1(Graphics g)
{
Rectangle rect=getBounds();
int X=(int)(rect.width/3.0);
int Y=(int)(rect.height/3.0);
for(int x=0;x<rect.width;x=x+X)
for(int y=0;y<rect.height;y=y+Y)
{
g.setColor(Color.red);
g.drawOval(x,y,X,Y);
}
}public void paint2(Graphics g)
{
Rectangle rect=getBounds();
int X=(int)(rect.width/3.0);
int Y=(int)(rect.height/3.0);
for(int x=0;x<rect.width;x=x+X)
for(int y=0;y<rect.height;y=y+Y)
{
g.setColor(Color.green);
g.drawRect(x,y,X,Y);
}
}
class RectangleandOvalHandler implements ActionListener
{
Component component;
public RectangleandOvalHandler(Component component)
{
this.component=component;
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
component.paint1(g); }
if(e.getSource()==button2)
{
component.paint2(g);
}
else if(e.getSource()==button3)
repaint();
}
} public static void main(String[] args)
{
RectangleandOval RO=new RectangleandOval();
RO.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
大家告诉我,我哪儿错了,我要崩溃了。
import javax.swing.*;
import java.awt.event.*;public class RectangleandOval extends JFrame
{
Rectangle rect;
JButton button1,button2,button3;
DrawPanel drawPanel;
public RectangleandOval()
{
super("Drawing Rectangle and Oval");
Container container=getContentPane();
JPanel panel=new JPanel();
container.add(panel,BorderLayout.NORTH);
button1=new JButton("Rectangle");
button2=new JButton("Oval");
button3=new JButton("Clear");
panel.add(button1);
panel.add(button2);
panel.add(button3);
drawPanel = new DrawPanel();
container.add(drawPanel, BorderLayout.CENTER); RectangleandOvalHandler handler=new RectangleandOvalHandler(drawPanel);
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
setSize(300,400);
show();
} private class DrawPanel extends JPanel
{
int flag = 0; protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (flag == 1)
{
paint1(g);
}
else if (flag == 2) {
paint2(g);
}
}
public void paint1(Graphics g)
{
Rectangle rect=getBounds();
int X=(int)(rect.width/3.0);
int Y=(int)(rect.height/3.0);
for(int x=0;x<rect.width;x=x+X)
for(int y=0;y<rect.height;y=y+Y)
{
g.setColor(Color.red);
g.drawOval(x,y,X,Y);
}
}
public void paint2(Graphics g)
{
Rectangle rect=getBounds();
int X=(int)(rect.width/3.0);
int Y=(int)(rect.height/3.0);
for(int x=0;x<rect.width;x=x+X)
for(int y=0;y<rect.height;y=y+Y)
{
g.setColor(Color.green);
g.drawRect(x,y,X,Y);
}
}
}
class RectangleandOvalHandler implements ActionListener
{
DrawPanel drawPanel;
public RectangleandOvalHandler(DrawPanel drawPanel)
{
this.drawPanel=drawPanel;
} public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
drawPanel.flag = 1;
drawPanel.repaint();
}
if(e.getSource()==button2)
{
drawPanel.flag = 2;
drawPanel.repaint();
}
else if(e.getSource()==button3) {
drawPanel.flag = 0;
drawPanel.repaint();
}
}
}
public static void main(String[] args)
{
RectangleandOval RO=new RectangleandOval();
RO.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
原贴内容:
你的程序中不应该在按钮的事件处理程序中主动调用paint1()或者paint2()方法来画图我自己写画图的方法不行吗?----------------------------------------------------------------
你的程序中应该覆盖paint(Graphics g)方法或者paintComponent(Graphics g)方法,然后在其中调用 paint1或者paint2来画图,按钮的事件处理程序中应该只是设置必要的标志和参数,然后调用repaint(),在repaint()内部会调用paint()或者paintComponent()方法的。这个过程好像有点绕弯子,多此一举,但其实这是由操作系统的底层的图形系统的处理方式决定的,我建议你找本<<java图形程序设计>>来看看,你会逐渐明白这其中的道理的。原贴内容:
我的另一个程序在(当中)http://community.csdn.net/Expert/topic/3924/3924855.xml?temp=.8114893
但是public void actionPerformed(ActionEvent evt)
{
component.setBackground(color);
component.repaint();
中的component中的方法setBackground();好象在Component中我没定义这个方法啊?为什么可以直接用。
----------------------------------------------------------------setBackground()方法是在java.awt.Component类中定义的,这个方法里面其实并没有直接的画图处理,它的处理过程和我前面说的类似,也是先把你传过去的颜色参数记录下来,然后调用repaint()重画。