用Java绘制多条线条

huangapple go评论57阅读模式
英文:

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&#39;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 &gt; 0) {
while(ya != yb &amp;&amp; time&gt;=count) {
ya++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(ya != yb &amp;&amp; time&gt;=count) {
ya--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else if(dya==0){
if(dxa &gt; 0) {
while(xa != xb&amp;&amp; time&gt;=count) {
xa++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(xa != xb&amp;&amp; time&gt;=count) {
xa--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else{
dxa = Math.abs(dxa);
dya = Math.abs(dya);
int pk = (dya &lt; dxa) ? 2*dya - dxa : 2*dxa - dya;
int dp = (dya &lt; dxa) ? 2*(dya-dxa) : 2*(dxa-dya);
boolean opr1 = (xa &gt; xb);
boolean opr2 = (ya &gt; yb);
while(xa != xb &amp;&amp; ya != yb &amp;&amp; time&gt;=count) {
if(pk &gt; 0) {
if(opr1) xa-=1;
else xa+=1;
if(opr2) ya-=1;
else ya+=1;
pk += dp;
}
else {
if (dya &lt; dxa) pk = pk + 2 * dya;
else pk = pk + 2 * dxa;
if(dxa&gt;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(&quot;Gambar&quot;);
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&#39;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&lt;Tugas1&gt; line = new LinkedList&lt;Tugas1&gt;();
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 &gt; 0) {
while(ya != yb &amp;&amp; time&gt;=count) {
ya++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(ya != yb &amp;&amp; time&gt;=count) {
ya--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else if(dya==0){
if(dxa &gt; 0) {
while(xa != xb&amp;&amp; time&gt;=count) {
xa++;
g.drawLine(a,b,xa,ya);
count++;
}
} else {
while(xa != xb&amp;&amp; time&gt;=count) {
xa--;
g.drawLine(a,b,xa,ya);
count++;
}
}
} else{
dxa = Math.abs(dxa);
dya = Math.abs(dya);
int pk = (dya &lt; dxa) ? 2*dya - dxa : 2*dxa - dya;
int dp = (dya &lt; dxa) ? 2*(dya-dxa) : 2*(dxa-dya);
boolean opr1 = (xa &gt; xb);
boolean opr2 = (ya &gt; yb);
while(xa != xb &amp;&amp; ya != yb &amp;&amp; time&gt;=count) {
if(pk &gt; 0) {
if(opr1) xa-=1;
else xa+=1;
if(opr2) ya-=1;
else ya+=1;
pk += dp;
}
else {
if (dya &lt; dxa) pk = pk + 2 * dya;
else pk = pk + 2 * dxa;
if(dxa&gt;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(&quot;Gambar&quot;);
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(&quot;Start&quot;);
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&lt;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&#39;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;
  1. Don't use static attributes unless you can explain why they need to be declared static.
  2. 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.
  3. Wrap the 4 numbers and color in a POJO (let's call it a ColoredLine) and add those to a LinkedList&lt;ColoredLine&gt;.
  4. Then iterate that list & paint each one in turn.

huangapple
  • 本文由 发表于 2020年9月30日 13:14:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64131301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定