简体   繁体   中英

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 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

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<ColoredLine> .
  4. Then iterate that list & paint each one in turn.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM