Muhammad Abdullah
Muhammad Abdullah

Reputation: 43

How to Invoke Paint Component Method in java

how to invoke paintComponent(Graphics g) method ??

method(){
Timer timer = new Timer();
timer.schedule(new Class(), 1000,1000);
}
public void run() {
//invoke PaintComponent 
}

Here! this is the updated one minimal code i want to change the end point of line every second simply how to invoke paintComponent(Graphics g) method ??

public class SimpleTest extends TimerTask {

JFrame frame;
int count ,x1,y1,x2,y2;
public void run() {

  count+=5;
  x1=count;
  y1=count;
  x2=count-1;
  y2=count-1;
  // repaint();   i want to invoke paintcomponent method from here , simply to change the end point of line every secd
}
void guiMethod(){
     frame=new JFrame("Libra's");
    frame.setBounds(50, 50, 250, 250);
    frame.setVisible(true);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new NewPanel());
}
public static void main(String[] args){
SimpleTest c=new SimpleTest();
   c.guiMethod();

   Timer timer = new Timer();
   timer.schedule(new SimpleTest(), 1000,1000);
}
}
class NewPanel extends JPanel{
SimpleTest obj=new SimpleTest();
@Override
protected void paintComponent(Graphics g){

  Graphics2D g2=(Graphics2D)g;
  g2.setStroke(new BasicStroke(3));

  g.drawLine(120, 120, 70, 80);


  g.drawLine(120, 120, obj.x1, obj.y1);

  g.drawLine(120, 120, obj.x2, obj.y2);
}}

Upvotes: 0

Views: 520

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

Here's the GUI I created, based on your GUI.

GUI

I made many changes to your code.

  1. I unpacked your code into separate GUI, JPanel, and TimerTask classes. This unpacking makes it easier to test each part of your Java application in isolation.

  2. I wrapped your main method code into a Runnable, and called the SwingUtilities invokeLater method to start your Swing GUI. The invokeLater method puts the creation and execution of the Swing components on the Event Dispatch thread. Oracle and I demand that you start every Swing application with a call to the invokeLater method.

  3. In the guiMethod method, I rearranged the JFrame code so it would execute in the proper order. The setVisible method is called last. I used the pack method to create the JFrame. You shouldn't specify Swing component sizes at all, except when you create a drawing panel. There, you set the size of the drawing panel like I did in your MyPanel class.

  4. I created a setEndPoints method in your NewPanel class so I could pass the end points from your Timer Task to the JPanel.

  5. I added a call to the super paintComponent in your paintComponent method. This ensures that the drawing panel is cleared and the Swing paint chain is unbroken and complete.

  6. I added another invokeLater method to your Timer Task. This ensures that the JPanel is repainted on the Event Dispatch thread.

Here's the code. I hope these changes and descriptions help you to understand using a Timer Task better.

package com.ggl.testing;

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SimpleTest {

    private JFrame frame;
    private NewPanel newPanel;

    public void guiMethod() {
        frame = new JFrame("Libra's");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        newPanel = new NewPanel();
        newPanel.setEndPoints(200, 100, 100, 200);
        frame.add(newPanel);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                SimpleTest c = new SimpleTest();
                c.guiMethod();

                Timer timer = new Timer();
                timer.schedule(c.new SimpleTimerTask(), 1000, 1000);
            }
        };
        SwingUtilities.invokeLater(runnable);
    }

    public class NewPanel extends JPanel {
        private static final long serialVersionUID = -4695412639313981349L;

        private int x1, y1, x2, y2;

        public NewPanel() {
            this.setPreferredSize(new Dimension(250, 250));
        }

        public void setEndPoints(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(new BasicStroke(3));
            g.drawLine(75, 75, 10, 10);
            g.drawLine(75, 75, x1, y1);
            g.drawLine(75, 75, x2, y2);
        }
    }

    public class SimpleTimerTask extends TimerTask {        
        int count;

        @Override
        public void run() {
            count += 5;
            final int x1 = 200 + count;
            final int y1 = 100 + count;
            final int x2 = 100 + count;
            final int y2 = 200 + count;
            // repaint(); i want to invoke paint component method from here , simply
            // to change the end point of line every second
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    newPanel.setEndPoints(x1, y1, x2, y2);
                    newPanel.repaint();
                }
            };
            SwingUtilities.invokeLater(runnable);
        }

    }

}

Upvotes: 1

Related Questions