在坐标系上绘制JAVA GUI

时间:2015-10-21 03:58:13

标签: java swing drawing paintcomponent shapes

Coordinate System

我有一个名为" GridPanel"在画面上绘制坐标系。

public class GridPanel extends JPanel
{
  protected paintComponent(Graphics g){
          // draws the Coordinate System with grid
  }
}

另一个名为" Shapes"在坐标系上绘制自定义形状。

public class Shape extends JComponent{
          protected paintComponent(Graphics g){
               // draws the shape on to the coordinate system
          }
    }

我正在寻找一种在shape类中进行绘画的方法,而不必一次又一次地绘制坐标系。

1 个答案:

答案 0 :(得分:2)

“如何”将归结为“什么”。

一般来说,如果你自己动手,你会发现在GridPane上绘制内容更容易,而不是依赖布局管理器之类的东西。

例如。这创建了一个简单的interface,其中有一个方法用于将内容绘制到网格上。

public interface GridShape {
    public void draw(Graphics2D g2d, JComponent parent);
}

然后通过想要绘制到网格上的内容来实现

public class WaveShape implements GridShape {

    @Override
    public void draw(Graphics2D g2d, JComponent parent) {
        g2d.setColor(Color.RED);

        int xDiff = parent.getWidth() / 4;
        int height = parent.getHeight() - 1;

        int xPos = 0;

        GeneralPath path = new GeneralPath();
        path.moveTo(0, 0);
        path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);
        xPos += xDiff;
        path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);
        xPos += xDiff;
        path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);
        xPos += xDiff;
        path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);
        g2d.draw(path);
    }

}

然后GridPane自我绘画,然后绘制它所具有的“形状”(这个例子非常基本,但是你可以设置一个改变绘制的“形状”的设定器,或者,如果需要,有一个List,允许你同时绘制多个形状)

WaveForm

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestGrid {

    public static void main(String[] args) {
        new TestGrid();
    }

    public TestGrid() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new GridPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class GridPane extends JPanel {

        private WaveShape waveShape;

        public GridPane() {
            waveShape = new WaveShape();
        }

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

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
            g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
            g2d.dispose();

            // I don't trust you
            g2d = (Graphics2D) g.create();
            waveShape.draw(g2d, this);
            g2d.dispose();
        }

    }

    public interface GridShape {
        public void draw(Graphics2D g2d, JComponent parent);
    }

    public class WaveShape implements GridShape {

        @Override
        public void draw(Graphics2D g2d, JComponent parent) {
            g2d.setColor(Color.RED);

            int xDiff = parent.getWidth() / 4;
            int height = parent.getHeight() - 1;

            int xPos = 0;

            GeneralPath path = new GeneralPath();
            path.moveTo(0, 0);
            path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);
            xPos += xDiff;
            path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);
            xPos += xDiff;
            path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);
            xPos += xDiff;
            path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);
            g2d.draw(path);
        }

    }

}