Home > other >  Add new data to Graphics2D panel in a Java Swing application
Add new data to Graphics2D panel in a Java Swing application

Time:12-24

I am writing a Java Swing application for data processing. one of the functions I need to add is to visualize the data in a graphical way. For this I want to use the Graphics2D class.

I have a GUI created, integrated my program and also a panel that draws the graphics using the Graphics2D class.

But my problem is that I can't figure out how to call the drawLine method after selecting and loading a file from the GUI

Below is short code example, showing my issue. It just contains a basic GUI with 2 panels and a menu with load option to explain my problem:

In de MyFrame.java file, I put a comment at line 87 to show exactly where I am stuck.

The appl is based on 3 files: main: here it creates an instance of MyFrame of the GUI Myframe: creates the GUI and further process of data MyPanel: makes a Jpanel of the Graphics2D with a base blue rectangle frame as start view.

If Anyone could give me a hint on how to call this drawLine method from outside the MyFrame() constructor...

I still don't fully understand the whole point on how to interact between classes...

here is a picture of what the GUI looks: enter image description here

Here's the revised GUI after "reading the file".

enter image description here

I created an application model to hold the line segments. This model is passed to the drawing JPanel so that the line segments can be drawn in the paintComponent method of the drawing JPanel.

I cleaned up your GUI. I used Swing layout managers to create the GUI. I separated the creation of the JPanels from the creation of the JFrame so the code is easier for people to read and understand.

Here's the complete runnable code. I made the additional classes inner classes so I could post this code as one block.

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;

public class ExampleDrawingGUI {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ExampleDrawingGUI().new MyFrame());
    }

    public class MyFrame extends JFrame {

        private static final long serialVersionUID = 1L;

        private ExampleDrawingModel model;
        
        JTextComponent tc;
        
        MyPanel p1;

        public MyFrame() {
            super("My Frame");
            this.model = new ExampleDrawingModel();
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setJMenuBar(createMenuBar());
            
            p1 = new MyPanel(model);
            this.add(p1, BorderLayout.CENTER);
            this.add(createTextPanel(), BorderLayout.SOUTH);
            
            this.pack();
            this.setLocationByPlatform(true);
//          this.setResizable(false);
            this.setVisible(true);
        }
        
        private JMenuBar createMenuBar() {
            JMenuBar mb = new JMenuBar();
            JMenu fm = new JMenu("File");
            JMenuItem loadItem = new JMenuItem("Load file");
            loadItem.addActionListener(e -> {
                tc.setText("loading"   "\n");
                model.readFile();
                p1.repaint();
            });
            fm.add(loadItem);
            mb.add(fm);
            
            return mb;
        }
        
        private JPanel createTextPanel() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            
            tc = new JTextPane();
            tc.setPreferredSize(new Dimension(450, 50));
            JScrollPane sp = new JScrollPane(tc);
            panel.add(sp, BorderLayout.CENTER);
            
            return panel;
        }
        
        public void repaint() {
            p1.repaint();
        }

    }

    public class MyPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        
        private ExampleDrawingModel model;

        public MyPanel(ExampleDrawingModel model) {
            this.model = model;
            this.setPreferredSize(new Dimension(450, 200));
        }

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

            Graphics2D g2D = (Graphics2D) g;
            paintBorder(g2D);
            
            for (LineSegment line : model.getLines()) {
                Point startPoint = line.getStartPoint();
                Point endPoint = line.getEndPoint();
                g2D.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
            }
        }

        private void paintBorder(Graphics2D g2D) {
            int margin = 5;
            int x1 = margin;
            int x2 = getWidth() - margin;
            int y1 = margin;
            int y2 = getHeight() - margin;
            
            g2D.setStroke(new BasicStroke(3f));
            g2D.setPaint(Color.blue);
            g2D.drawLine(x1, y1, x1, y2);
            g2D.drawLine(x1, y1, x2, y1);
            g2D.drawLine(x2, y1, x2, y2);
            g2D.drawLine(x1, y2, x2, y2);
        }

    }
    
    public class ExampleDrawingModel {
        
        private List<LineSegment> lines;
        
        public ExampleDrawingModel() {
            this.lines = new ArrayList<>();
        }
        
        public void readFile() {
            this.lines.clear();
            // Here's where you'd read a file and create a list of lines.
            lines.add(new LineSegment(new Point(100, 100), new Point(100, 150)));
        }

        public List<LineSegment> getLines() {
            return lines;
        }
        
    }
    
    public class LineSegment {
        
        private final Point startPoint, endPoint;

        public LineSegment(Point startPoint, Point endPoint) {
            this.startPoint = startPoint;
            this.endPoint = endPoint;
        }

        public Point getStartPoint() {
            return startPoint;
        }

        public Point getEndPoint() {
            return endPoint;
        }
        
    }

}
  • Related