简体   繁体   中英

Java repaint() not working

I am making a simple program to paint a graph and some points in it. The points should be made with methods while changing coordinates of the g.fillOval but actually its painting only the last point.

Here is the code:

import javax.swing.*;
import java.awt.*;
public class PointGraphWriter extends JPanel
{
   JFrame korniza = new JFrame();
   private int x;
   private int y;
   private int length;
   private String OX;
   private String OY;
   private String emri;
   private int y_height;
   private int x_num;

   public PointGraphWriter()
   {
      int width= 500;
      korniza.setSize(width,width);
      korniza.setVisible(true);
      korniza.setTitle(emri);
      korniza.getContentPane().add(this);

   }

   public void paintComponent(Graphics g)
   {
      g.drawLine(x,y,x+length,y);
      g.drawLine(x,y,x,y-length);
      g.drawString(OX,x+length, y+15);
      g.drawString(OY,x-15,y-length);
      g.drawString("0", x -15,y);
      g.drawString("0", x,y+15);
      g.fillOval(x_num,y-y_height-2, 4 ,4);
   }

   public void setTitle(String name)
   {
      emri= name;
      this.repaint();
   }

   public void setAxes(int x_pos, int y_pos, int axis_length, String x_label, String y_label)
   {
      x= x_pos;
      y=y_pos;
      length= axis_length;
      OX = x_label;
      OY = y_label;   
   }

   public void setPoint1(int height)
   {
      y_height=height;
      x_num = x-2;
      this.repaint();
   }

   public void setPoint2(int height)
   {
      y_height=height;
      x_num = x + length/5-2;
      this.repaint();
   }   
}   

and here is the main method:

public class TestPlot
{
   public static void main(String[] a)
   { 
      PointGraphWriter e = new PointGraphWriter();
      e.setTitle("Graph of y = x*x");
      e.setAxes(50, 110, 90, "5", "30");
      int scale_factor = 3;
      e.setPoint1(0 * scale_factor); 
      e.setPoint2(1 * scale_factor);
   }
}

Please have a look at this example. Something in lines of this, you might have to incorporate in your example, to make it work. Simply use a Collection to store what you have previously painted, and when the new thingy comes along, simply add that thingy to the list, and repaint the whole Collection again. As shown below :

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class RectangleExample {

    private DrawingBoard customPanel;
    private JButton button;

    private Random random;

    private java.util.List<Rectangle2D.Double> rectangles;

    private ActionListener buttonActions = 
                        new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            Rectangle2D.Double rectangle = new Rectangle2D.Double(
                        (double) random.nextInt(100), (double) random.nextInt(100),
                        (double) random.nextInt(100), (double) random.nextInt(100));
            rectangles.add(rectangle);
            customPanel.setValues(rectangles);
        }
    };

    public RectangleExample() {
        rectangles = new ArrayList<Rectangle2D.Double>();
        random = new Random();
    }   

    private void displayGUI() {
        JFrame frame = new JFrame("Rectangle Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));

        customPanel = new DrawingBoard();
        contentPane.add(customPanel, BorderLayout.CENTER);

        button = new JButton("Create Rectangle");
        button.addActionListener(buttonActions);
        contentPane.add(button, BorderLayout.PAGE_END);

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

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new RectangleExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

class DrawingBoard extends JPanel {

    private java.util.List<Rectangle2D.Double> rectangles = 
                                new ArrayList<Rectangle2D.Double>();

    public DrawingBoard() {
        setOpaque(true);
        setBackground(Color.WHITE);
    }

    public void setValues(java.util.List<Rectangle2D.Double> rectangles) {
        this.rectangles = rectangles;
        repaint();
    }

    @Override
    public Dimension getPreferredSize() {
        return (new Dimension(300, 300));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Rectangle2D.Double rectangle : rectangles) {
            g.drawRect((int)rectangle.getX(), (int)rectangle.getY(),
                    (int)rectangle.getWidth(), (int)rectangle.getHeight());
        }
        System.out.println("WORKING");
    }
}

See Custom Painting Approaches for examples of the two common ways to do painting:

  1. Keep a List of the objects to be painted
  2. Paint onto a BufferedImage

The approach you choose will depend on your exact requirement.

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