在下面的Java程序中有什么方法可以加快绘图过程吗?
Is there any way to speed up the drawing process in the following Java program?
此程序使用 fillPolygon() 在屏幕上绘制正方形 method.On 执行代码,绘制部分非常慢。我该如何改进呢?还是使用 Java AWT 的 GUI 开发最差?
import java.awt.*;
import java.awt.event.*;
public class BluePolygon extends Frame {
int x, x1, y, y1;
public BluePolygon() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
x = me.getX();
y = me.getY();
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent me) {
x1 = me.getX();
y1 = me.getY();
repaint();
}
});
}
public void paint(Graphics g) {
g.setColor(new Color(Color.blue.getRed(),Color.blue.getGreen(),Color.blue.getBlue(),60));
int xx[] = {x1,x,x,x1};
int yy[] = {y,y,y1,y1};
g.fillPolygon(xx, yy, 4);
}
public static void main(String args[]) {
BluePolygon winapp = new BluePolygon();
winapp.setTitle("Blue Polygon");
winapp.setSize(1200, 720);
winapp.setVisible(true);
}
}
Or is it that GUI development using Java AWT is worst?
我会这么说。
您可以将其与 Swing 环境进行比较,至少在我的 PC 中,Swing 环境效果更好。
public class BluePolygon extends JPanel {
private static final long serialVersionUID = 1L;
int x, x1, y, y1;
public BluePolygon() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
x = me.getX();
y = me.getY();
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent me) {
x1 = me.getX();
y1 = me.getY();
repaint();
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(Color.blue.getRed(), Color.blue.getGreen(), Color.blue.getBlue(), 60));
int xx[] = { x1, x, x, x1 };
int yy[] = { y, y, y1, y1 };
g.fillPolygon(xx, yy, 4);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(() -> {
BluePolygon winapp = new BluePolygon();
winapp.setPreferredSize(new Dimension(400, 400));
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(winapp);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
}
此程序使用 fillPolygon() 在屏幕上绘制正方形 method.On 执行代码,绘制部分非常慢。我该如何改进呢?还是使用 Java AWT 的 GUI 开发最差?
import java.awt.*;
import java.awt.event.*;
public class BluePolygon extends Frame {
int x, x1, y, y1;
public BluePolygon() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
x = me.getX();
y = me.getY();
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent me) {
x1 = me.getX();
y1 = me.getY();
repaint();
}
});
}
public void paint(Graphics g) {
g.setColor(new Color(Color.blue.getRed(),Color.blue.getGreen(),Color.blue.getBlue(),60));
int xx[] = {x1,x,x,x1};
int yy[] = {y,y,y1,y1};
g.fillPolygon(xx, yy, 4);
}
public static void main(String args[]) {
BluePolygon winapp = new BluePolygon();
winapp.setTitle("Blue Polygon");
winapp.setSize(1200, 720);
winapp.setVisible(true);
}
}
Or is it that GUI development using Java AWT is worst?
我会这么说。
您可以将其与 Swing 环境进行比较,至少在我的 PC 中,Swing 环境效果更好。
public class BluePolygon extends JPanel {
private static final long serialVersionUID = 1L;
int x, x1, y, y1;
public BluePolygon() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
x = me.getX();
y = me.getY();
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent me) {
x1 = me.getX();
y1 = me.getY();
repaint();
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(Color.blue.getRed(), Color.blue.getGreen(), Color.blue.getBlue(), 60));
int xx[] = { x1, x, x, x1 };
int yy[] = { y, y, y1, y1 };
g.fillPolygon(xx, yy, 4);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(() -> {
BluePolygon winapp = new BluePolygon();
winapp.setPreferredSize(new Dimension(400, 400));
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(winapp);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
}