Java类Graphics和Graphics2D应该包含Zelle图形所包含的大部分内容。
我是Java的新手,我只想获得一些简单的东西,可能类似于Zelle的Python图形。
下面是一些Java Swing代码,让您开始学习。只要你按住鼠标左键,这个Java应用程序就会用鼠标绘制。当你释放鼠标左键时,屏幕会擦除,你可以画其他东西。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;
public class DrawingTest implements Runnable {
private JFrame frame;
private MyDrawPanel drawPanel;
private List<Point> points;
public DrawingTest() {
points = new ArrayList<Point>();
}
@Override
public void run() {
frame = new JFrame("Bouncing Vertices");
drawPanel = new MyDrawPanel(this);
MyListener alpha = new MyListener(this);
drawPanel.addMouseMotionListener(alpha);
drawPanel.addMouseListener(alpha);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(drawPanel);
frame.setSize(800, 600);
frame.setVisible(true);
}
public JPanel getDrawingPanel() {
return drawPanel;
}
public List<Point> getPoints() {
return points;
}
public void setPoint(int x, int y) {
points.add(new Point(x, y));
}
public void resetPoints() {
points.clear();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new DrawingTest());
}
private class MyListener extends MouseInputAdapter {
private DrawingTest drawingTest;
public MyListener(DrawingTest drawingTest) {
this.drawingTest = drawingTest;
}
@Override
public void mouseDragged(MouseEvent event) {
drawingTest.setPoint(event.getX(), event.getY());
drawingTest.getDrawingPanel().repaint();
}
@Override
public void mouseReleased(MouseEvent event) {
drawingTest.resetPoints();
}
}
private class MyDrawPanel extends JPanel {
private static final long serialVersionUID = 1L;
private DrawingTest drawingTest;
public MyDrawPanel(DrawingTest drawingTest) {
this.drawingTest = drawingTest;
}
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLUE);
for (int i = 1; i < drawingTest.getPoints().size(); i++) {
Point p1 = drawingTest.getPoints().get(i - 1);
Point p2 = drawingTest.getPoints().get(i);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
}
}
代码中的最后一个方法paintComponent方法是使用Graphics方法的地方。