1

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);
   }
}
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
Drilon Blakqori
  • 2,796
  • 2
  • 17
  • 25
  • You need to save the __Co-ordinates__ in some `Collections` and iterate on that `Collection` to draw all points each time(a new point is added to the previous list) – nIcE cOw Jan 09 '14 at 16:29
  • Ehm I'm actually a beginner don't really know how to do that. I just have this exercise as a project in my school. http://img856.imageshack.us/img856/8932/9n1b.png this is the exercise. at the moment i used only setPoint1 and setPoint 2 but i cant fix that problem. – Drilon Blakqori Jan 09 '14 at 16:35
  • You can have a look at this [thread](http://stackoverflow.com/q/11062249/1057230). I am afraid, I cann't do your homework for you. Though a small example will be coming soon..., if the idea is still unclear to you :-) – nIcE cOw Jan 09 '14 at 16:37
  • yeah i just thought i could use just the repaint() to do the job on there, but it seems like its not working. thanks anyway – Drilon Blakqori Jan 09 '14 at 16:46
  • 1
    Swing components can't be used from any other thread that the event dispatch thread. Wrap the code of the main method inside a SwingUtilities.invokeLater() call, and everything will go fine. http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html – JB Nizet Jan 09 '14 at 17:13

2 Answers2

2

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");
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
2

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.

camickr
  • 321,443
  • 19
  • 166
  • 288