英文:
Drawing multiple lines with Java
问题
I'm learning Computer Graphics and want to draw lines with Java Swing. I want to draw multiple lines which stays and come with random color and random point of x and y. I can draw a line smoothly without any problem with this code:
package tugas1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Tugas1 extends JPanel {
public static int time = 0;
public static int x1, y1, x2, y2;
Color randomcolor;
public Tugas1() {
this.x1 = (int) (Math.random() * 1280);
this.y1 = (int) (Math.random() * 720);
this.x2 = (int) (Math.random() * 1280);
this.y2 = (int) (Math.random() * 720);
randomcolor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
// Inisialisasi timer untuk animasi
javax.swing.Timer timer = new javax.swing.Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent e) {
//menambah value variabel time secara berkala
time++;
repaint();
}
});
timer.start();
}
public void garis(Graphics g, int xa, int ya, int xb, int yb){
int count = 0;
int a = xa;
int b = ya;
int dxa = ((xb - xa));
int dya = ((yb - ya));
g.setColor(randomcolor);
if(dxa==0){
if(dya > 0) {
while(ya != yb && time >= count) {
ya++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(ya != yb && time >= count) {
ya--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else if(dya==0){
if(dxa > 0) {
while(xa != xb && time >= count) {
xa++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(xa != xb && time >= count) {
xa--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else{
dxa = Math.abs(dxa);
dya = Math.abs(dya);
int pk = (dya < dxa) ? 2 * dya - dxa : 2 * dxa - dya;
int dp = (dya < dxa) ? 2 * (dya - dxa) : 2 * (dxa - dya);
boolean opr1 = (xa > xb);
boolean opr2 = (ya > yb);
while(xa != xb && ya != yb && time >= count) {
if(pk > 0) {
if(opr1) xa -= 1;
else xa += 1;
if(opr2) ya -= 1;
else ya += 1;
pk += dp;
}
else {
if (dya < dxa) pk = pk + 2 * dya;
else pk = pk + 2 * dxa;
if(dxa > dya){
if(opr1) xa -= 1;
else xa += 1;
}
else {
if(opr2) ya -= 1;
else ya += 1;
}
}
g.drawLine(a,b,xa,ya);
a = xa;
b = ya;
count++;
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.BLACK);
garis(g, x1, y1, x2, y2);
}
public static void main(String[] args) {
JFrame fr = new JFrame("Gambar");
fr.setSize(1280, 720);
Tugas1 panelGambar = new Tugas1();
fr.getContentPane().add(panelGambar);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setVisible(true);
}
}
But when I try to create a multiple line with just one click if a button, let say 100 lines, my button doesn't work (it just creates one line, and the animation disappears). Here is the lines of my code for creating multiple lines:
package tugas1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
public class Tugas1 extends JPanel {
public static int time = 0;
public static int x1, y1, x2, y2;
Color randomcolor;
public Tugas1(int x1, int y1, int x2, int y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.randomcolor = color;
// Inisialisasi timer untuk animasi
javax.swing.Timer timer = new javax.swing.Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent e) {
//menambah value variabel time secara berkala
time++;
repaint();
}
});
timer.start();
}
public final LinkedList<Tugas1> line = new LinkedList<Tugas1>();
public void garis(Graphics g, int xa, int ya, int xb, int yb){
int count = 0;
int a = xa;
int b = ya;
int dxa = ((xb - xa));
int dya = ((yb - ya));
g.setColor(randomcolor);
if(dxa==0){
if(dya > 0) {
while(ya != yb && time >= count) {
ya++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(ya != yb && time >= count) {
ya--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else if(dya==0){
if(dxa > 0) {
while(xa != xb && time >= count) {
xa++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(xa != xb && time >= count) {
xa--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else{
dxa = Math.abs(dxa);
dya = Math.abs(dya);
int pk = (dya < dxa) ? 2 * dya - dxa : 2 * dxa - dya;
int dp = (dya < dxa) ? 2 * (dya - dxa) : 2 * (dxa - dya);
boolean opr1 = (xa > xb);
boolean opr2 = (ya > yb);
while(xa != xb && ya != yb && time
<details>
<summary>英文:</summary>
I'm learning Computer Graphics and want to draw lines with Java Swing. I want to draw multiple lines which stays and come with random color and random point of x and y. I can draw a line smoothly without any problem with this code:
package tugas1;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Tugas1 extends JPanel{
public static int time = 0;
public static int x1,y1,x2,y2;
Color randomcolor;
public Tugas1() {
this.x1 = (int) (Math.random()*1280);
this.y1 = (int) (Math.random()*720);
this.x2 = (int) (Math.random()*1280);
this.y2 = (int) (Math.random()*720);
randomcolor = new Color((float)Math.random(),(float)Math.random(),(float)Math.random());
// Inisialisasi timer untuk animasi
javax.swing.Timer timer = new javax.swing.Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent e) {
//menambah value variabel time secara berkala
time++;
repaint();
}
});
timer.start();
}
public void garis(Graphics g, int xa, int ya, int xb, int yb){
int count = 0;
int a = xa;
int b = ya;
int dxa = ((xb - xa));
int dya = ((yb - ya));
g.setColor(randomcolor);
if(dxa==0){
if(dya > 0) {
while(ya != yb && time>=count) {
ya++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(ya != yb && time>=count) {
ya--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else if(dya==0){
if(dxa > 0) {
while(xa != xb&& time>=count) {
xa++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(xa != xb&& time>=count) {
xa--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else{
dxa = Math.abs(dxa);
dya = Math.abs(dya);
int pk = (dya < dxa) ? 2*dya - dxa : 2*dxa - dya;
int dp = (dya < dxa) ? 2*(dya-dxa) : 2*(dxa-dya);
boolean opr1 = (xa > xb);
boolean opr2 = (ya > yb);
while(xa != xb && ya != yb && time>=count) {
if(pk > 0) {
if(opr1) xa-=1;
else xa+=1;
if(opr2) ya-=1;
else ya+=1;
pk += dp;
}
else {
if (dya < dxa) pk = pk + 2 * dya;
else pk = pk + 2 * dxa;
if(dxa>dya){
if(opr1) xa-=1;
else xa+=1;
}
else {
if(opr2) ya-=1;
else ya+=1;
}
}
g.drawLine(a,b,xa,ya);
a = xa;
b = ya;
count++;
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.BLACK);
garis(g, x1, y1, x2, y2);
}
public static void main(String[] args) {
JFrame fr = new JFrame("Gambar");
fr.setSize(1280,720);
Tugas1 panelGambar = new Tugas1();
fr.getContentPane().add(panelGambar);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setVisible(true);
}
}
But when I try to create a multiple line with just one click if a button, let say 100 lines, my button doesn't work (it just create one line, and the animation is dissappear). Here is the lines of my code for creating multiple lines:
package tugas1;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
public class Tugas1 extends JPanel{
public static int time = 0;
public static int x1,y1,x2,y2;
Color randomcolor;
public Tugas1(int x1, int y1, int x2, int y2, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.randomcolor = color;
// Inisialisasi timer untuk animasi
javax.swing.Timer timer = new javax.swing.Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent e) {
//menambah value variabel time secara berkala
time++;
repaint();
}
});
timer.start();
}
public final LinkedList<Tugas1> line = new LinkedList<Tugas1>();
public void garis(Graphics g, int xa, int ya, int xb, int yb){
int count = 0;
int a = xa;
int b = ya;
int dxa = ((xb - xa));
int dya = ((yb - ya));
g.setColor(randomcolor);
if(dxa==0){
if(dya > 0) {
while(ya != yb && time>=count) {
ya++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(ya != yb && time>=count) {
ya--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else if(dya==0){
if(dxa > 0) {
while(xa != xb&& time>=count) {
xa++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(xa != xb&& time>=count) {
xa--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else{
dxa = Math.abs(dxa);
dya = Math.abs(dya);
int pk = (dya < dxa) ? 2*dya - dxa : 2*dxa - dya;
int dp = (dya < dxa) ? 2*(dya-dxa) : 2*(dxa-dya);
boolean opr1 = (xa > xb);
boolean opr2 = (ya > yb);
while(xa != xb && ya != yb && time>=count) {
if(pk > 0) {
if(opr1) xa-=1;
else xa+=1;
if(opr2) ya-=1;
else ya+=1;
pk += dp;
}
else {
if (dya < dxa) pk = pk + 2 * dya;
else pk = pk + 2 * dxa;
if(dxa>dya){
if(opr1) xa-=1;
else xa+=1;
}
else {
if(opr2) ya-=1;
else ya+=1;
}
}
g.drawLine(a,b,xa,ya);
a = xa;
b = ya;
count++;
}
}
}
public void addLine(int x1, int y1, int x2, int y2, Color color) {
line.add(new Tugas1(x1, y1, x2, y2, color));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.BLACK);
for (Tugas1 lines : line){
garis(g, lines.x1, lines.y1, lines.x2, lines.y2);
}
}
public static void main(String[] args) {
JFrame fr = new JFrame("Gambar");
fr.setSize(1280,720);
final Tugas1 panelGambar = new Tugas1(0,0,0,0, Color.WHITE);
fr.getContentPane().add(panelGambar);
JPanel buttonsPanel = new JPanel();
JButton startButton = new JButton("Start");
buttonsPanel.add(startButton);
fr.getContentPane().add(buttonsPanel, BorderLayout.PAGE_END);
startButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
for(int i= 0; i<100; i++){
int x1 = (int) (Math.random()*1280);
int y1 = (int) (Math.random()*720);
int x2 = (int) (Math.random()*1280);
int y2 = (int) (Math.random()*720);
Color randomColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
panelGambar.addLine(x1, y1, x2, y2, randomColor);
}
}
});
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setVisible(true);
}
}
Maybe there's a way to keep the lines drawn are not dissapear and all lines are drawn as the first template of code
</details>
# 答案1
**得分**: 1
```java
1. 除非你能解释为什么需要将属性声明为`static`,否则不要使用`static`属性。
2. 这些属性被声明为`Tugas1`类的一部分。它是一个面板。它不需要这些属性,这也是问题的一部分。移除它们。
3. 将这4个数字和颜色封装在一个POJO中(我们称其为`ColoredLine`),并将其添加到一个`LinkedList<ColoredLine>`中。
4. 然后迭代*该*列表并依次绘制每一个。
英文:
public static int x1,y1,x2,y2;
- Don't use
static
attributes unless you can explain why they need to be declared static. - These attributes are declared as part of the
Tugas1
class. It's a panel. It does not need these attributes and this is part of the problem. Remove them. - Wrap the 4 numbers and color in a POJO (let's call it a
ColoredLine
) and add those to aLinkedList<ColoredLine>
. - Then iterate that list & paint each one in turn.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论