我的項目出現問題,我的項目是繪製線條(喜歡在Windows中繪製)。我想用mouseDragged,mousePressed和mouseReleased繪製更多的一行。但是,當我運行測試,它表現出了很多的錯誤,在這裏我的代碼在java中使用mouseevent繪製線條
package image;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class paint extends JFrame{
private Point points[] = new Point[10000];
private Point pointends[] = new Point[10000];
private int pointCount = 0;
public paint()
{
panel paint2 = new panel();
add(paint2,BorderLayout.CENTER);
}
private class panel extends JPanel
{
public panel()
{
setBackground(Color.BLUE);
MouseHandler handler = new MouseHandler();
this.addMouseMotionListener(handler);
this.addMouseListener(handler);
}
@Override
protected void paintComponent(Graphics g)
{
// TODO Auto-generated method stub
super.paintComponent(g);
for(int i = 0;i < pointCount;i++)
{
g.setColor(Color.RED);
g.drawLine(points[pointCount].x, points[pointCount].y, pointends[pointCount].x, pointends[pointCount].y);
}
}
}
private class MouseHandler extends MouseAdapter
{
@Override
public void mouseDragged(MouseEvent e)
{
// TODO Auto-generated method stub
pointends[ pointCount ] = e.getPoint();
repaint();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
super.mousePressed(e);
if(pointCount < points.length)
{
points[ pointCount ] = e.getPoint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseReleased(e);
pointends[pointCount]=e.getPoint();
repaint();
pointCount++;
}
}
}
,這裏是我的無效的主要
package image;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.BorderLayout;
public class test
{
public static void main(String[] args) {
paint paint1 = new paint();
/*paintP.add(paint1, BorderLayout.CENTER);
paintP.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
paintP.setSize(400,400);
paintP.setVisible(true);*/
paint1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
paint1.setSize(400,400);
paint1.setVisible(true);
}
}
**很多的錯誤**?編譯時間?運行 ?請告訴我們錯誤! –
[Java編碼標準§9](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367)建議類名稱應以大寫字母開頭。 –