實作小畫家遭遇的問題
以下是我的程式碼
-----------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class painter extends JFrame implements
MouseListener,MouseMotionListener {
int x = 0;
int y = 0;
int pen = 1;
boolean ispress = false;
int setcolor = 1;
Color C;
private JButton b1,b2,b3,b4,b5,b6;
public painter() {
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
Container container1 = getContentPane();
container1.addMouseListener(this);
container1.addMouseMotionListener(this);
JPanel p1 = new JPanel();
}
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
ispress = true;
repaint();
}
public void mouseReleased(MouseEvent e) {
x = e.getX();
y = e.getY();
ispress = false;
repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void paint(Graphics g)
{
//滑鼠左鍵要按下才會畫
if(ispress){
g.setColor(Color.black);
g.fillOval(x+5,y+30,5,5);//以橢圓代替畫筆
}
}
public static void main(String[] args) {
painter frame = new painter();
frame.setTitle("TEST2");
frame.setSize(400, 300);
frame.setLocation(900,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
-----------------------------------------------------------------------
在實際操作時發現會有不連續的問題
如此圖所示: http://0rz.tw/RxIWp
不知道問題出在哪裡
請各位幫忙一下 感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.249.133.137
→
10/15 14:10, , 1F
10/15 14:10, 1F
推
10/15 20:48, , 2F
10/15 20:48, 2F
java 近期熱門文章
PTT數位生活區 即時熱門文章