英文:
Is there any way to speed up the drawing process in the following Java program?
问题
这个程序使用fillPolygon()
方法在屏幕上绘制一个正方形。在执行代码时,绘图部分非常缓慢。我如何改进这个?或者是不是使用 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);
}
}
英文:
This program draws a square on screen using fillPolygon() method.On executing the code, the drawing part is very slow. How can i improve this? Or is it that GUI development using Java AWT is worst?
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);
}
}
答案1
得分: 0
>还是说使用Java AWT进行GUI开发最糟糕?
我会这么说。
你可以将其与Swing环境进行比较,在我至少的个人电脑上,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);
});
}
}
英文:
> Or is it that GUI development using Java AWT is worst?
I'd say so.
You can compare it with a Swing environment, which in my PC at least, works better.
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);
});
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论