我试图在框架上绘制每个像素,但是我无法算出如何绘制下一个像素,它仅显示一个像素绘制,PLZ帮助。我不知道如何实现此目标会编辑此代码,以便它在相邻坐标中绘制像素。
import java.lang.*;
import java.util.*;
import java.util.List;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class MyDrawPixel extends Frame {
public Point mypoint = new Point();
public static void drawPixel(Graphics g, int x, int y, int size, Paint color)
{
Graphics2D ga = (Graphics2D)g;
Shape circle = new Ellipse2D.Float(x, y, size, size);
ga.setPaint(color);
ga.draw(circle);
ga.setPaint(color);
ga.fill(circle);
}
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D)g;
drawPixel(g, mypoint.x, mypoint.y, 1, Color.black);
}
public static void main(String args[])
{
MyDrawPixel frame = new MyDrawPixel();
frame.mypoint.x = 43;
frame.mypoint.y = 43;
MyDrawPixel frame1 = new MyDrawPixel();
frame1.mypoint.x = 44;
frame1.mypoint.y = 44;
MyDrawPixel frame2 = new MyDrawPixel();
frame2.mypoint.x = 45;
frame2.mypoint.y = 45;
MyDrawPixel frame3 = new MyDrawPixel();
frame3.mypoint.x = 46;
frame3.mypoint.y = 46;
MyDrawPixel frame4 = new MyDrawPixel();
frame4.mypoint.x = 47;
frame4.mypoint.y = 47;
frame.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
您正在创建5帧,并且仅显示一个。您需要做的是创建1帧并正确显示。
您可以使用 repaint()
刷新屏幕,然后如果要移动单个点,只需更改重新粉刷之间的属性。
甚至更好,找到有关自定义绘画的适当教程,您的代码非常可怕。