Java 为何我的repaint调不出paint吖?
刚刚开始学Java的GUI..想写一个program,能够在鼠标点击的位置画一个半径为10的圆圈.结果我试了好久原来是repaint没有把paint调出来..以下是我的c...
刚刚开始学Java的GUI.. 想写一个program, 能够在鼠标点击的位置画一个半径为10的圆圈.
结果我试了好久原来是repaint没有把paint调出来..
以下是我的code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Abcd
{
public static void main (String[] args)
{
DrawFrame frame = new DrawFrame ();
frame.addWindowListener
(new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
);
}
}
class DrawFrame extends JFrame
{
Painting i;
public DrawFrame ()
{
super ("Mouse Drawing");
addMouseListener (new MouseAdapter ()
{
public void mouseClicked (MouseEvent e)
{
i = new Painting (e.getX (), e.getY ());
}
}
);
setSize (600, 600);
setVisible (true);
}
}
class Painting extends JComponent
{
private int x;
private int y;
static final int RADIUS = 10;
public Painting (int getX, int getY)
{
x = getX;
y = getY;
repaint ();
}
public void paint (Graphics g)
{
g.fillOval (x - RADIUS, y - RADIUS, 2 * RADIUS, 2 * RADIUS);
}
}
我知道如果把Painting那一个class的内容都放到DrawFrame里面的话是可以成功的, 我试过的确可以,
但我就是不想把Painting的内容写在DrawFrame里面,所以我就另外创了这个Painting的class,
可是结果repaint没有把paint调出来..
有哪位大神能解答一下, 感激不尽! 展开
结果我试了好久原来是repaint没有把paint调出来..
以下是我的code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Abcd
{
public static void main (String[] args)
{
DrawFrame frame = new DrawFrame ();
frame.addWindowListener
(new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
);
}
}
class DrawFrame extends JFrame
{
Painting i;
public DrawFrame ()
{
super ("Mouse Drawing");
addMouseListener (new MouseAdapter ()
{
public void mouseClicked (MouseEvent e)
{
i = new Painting (e.getX (), e.getY ());
}
}
);
setSize (600, 600);
setVisible (true);
}
}
class Painting extends JComponent
{
private int x;
private int y;
static final int RADIUS = 10;
public Painting (int getX, int getY)
{
x = getX;
y = getY;
repaint ();
}
public void paint (Graphics g)
{
g.fillOval (x - RADIUS, y - RADIUS, 2 * RADIUS, 2 * RADIUS);
}
}
我知道如果把Painting那一个class的内容都放到DrawFrame里面的话是可以成功的, 我试过的确可以,
但我就是不想把Painting的内容写在DrawFrame里面,所以我就另外创了这个Painting的class,
可是结果repaint没有把paint调出来..
有哪位大神能解答一下, 感激不尽! 展开
1个回答
展开全部
package baiduandtext;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Abcd {
public static void main(String[] args) {
DrawFrame frame = new DrawFrame();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class DrawFrame extends JFrame {
public DrawFrame() {
super("Mouse Drawing");
final Painting i = new Painting();
this.add(i);
i.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
i.paint(e.getX(), e.getY());
}
});
setSize(600, 600);
setVisible(true);
}
}
class Painting extends JComponent
{
private int x;
private int y;
static final int RADIUS = 10;
public void paint(int getX, int getY) {
x = getX;
y = getY;
repaint();
}
@Override
public void paintComponent(Graphics g) {
g.fillOval(x - RADIUS, y - RADIUS, 2 * RADIUS, 2 * RADIUS);
}
}
更多追问追答
追问
哦原来是这样!那如果我想每画一个圆之前的圆都不会消失那要怎麽做呢?谢谢!
哦原来是这样!那如果我想每画一个圆之前的圆都不会消失那要怎麽做呢?谢谢!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询