试图用JPanel绘制线条

时间:2013-04-25 04:30:08

标签: java swing jpanel java-2d paintcomponent

我正在尝试使用JPanel绘制线条而且我已经打了一些墙。我可以让双方失望但是一旦从x线中减去它就会出错。

lines i'm tring to draw

package GUIstuff;
import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawPanel extends JPanel{

public void paintComponent (Graphics g){

    super.paintComponent(g);

    int width = getWidth();
    int height = getHeight();

    int drawCounter = 0; // counters for all the while statements 
    int drawCounter2 = 0;
    int drawCounter3 = 0;
    int drawCounter4 = 0;



    int x1 = 0; // cords change with the while statemetns
    int x2 = 0;
    int y1 = 0;
    int y2 = 0;     
    while (drawCounter <= 15){ // counter 
    y2 = 250;
    g.drawLine(x1, y1, x2, y2);
    x2 = x2 + 15;
    y1 = y1 + 15;
    drawCounter++;  } 


    int u1 = 0;
    int u2 = 0;
    int v1 = 0;
    int v2 = 0;
    while (drawCounter2 <= 15){
    u2 = 250;
    g.drawLine(u1, v1, u2, v2);
    u1 = u1 + 15;
    v2 = v2 + 15;
    drawCounter2++; 
    }

    int a1 = 0;
    int a2 = 0;
    int b1 = 0;
    int b2 = 0;

    while (drawCounter3 <= 15){
    a2 = 250;
    g.drawLine(a1, b1, a2, b2);
    b1 = b1 + 15;
    a2 = a2 - 15;
    drawCounter3++;

    }
 }
}

这是我的跑步者课程

package GUIstuff;
import javax.swing.JFrame;


public class DrawPanelTest {

public static void main (String args[]){

    DrawPanel panel = new DrawPanel();

    JFrame application = new JFrame();

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    application.add(panel);
    application.setSize (250, 250);
    application.setVisible(true);


}

}

我在左下角和右上角有一条线,但是当我尝试从x减去时,我只是让整条线穿过。

2 个答案:

答案 0 :(得分:6)

在进行自定义绘制时,您应该覆盖getPreferredSize()方法,以便面板可以按其首选大小显示。

绘制线条时,两个变量相同,两个变量不同。适当时使用width / height变量而不是硬编码数字。在下面的例子中,我做了左侧和下侧。底部显示了如何减去。我会让你弄清楚另外两方的模式。

此外,我使面板更加动态,因此可以轻松配置想要绘制的线条数量和线条之间的间隙。

import java.awt.*;
import javax.swing.*;

public class DrawSSCCE extends JPanel
{
    private int lines;
    private int lineGap;

    public DrawSSCCE(int lines, int lineGap)
    {
        this.lines = lines;
        this.lineGap = lineGap;
    }

    @Override
    public Dimension getPreferredSize()
    {
        int size = lines * lineGap;
        return new Dimension(size, size);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        int width = getWidth();
        int height = getHeight();

        //  Draw lines starting from left to bottom

        int x = lineGap;
        int y = 0;

        for (int i = 0; i < lines; i++)
        {
            g.drawLine(0, y, x, height);
            x += lineGap;
            y += lineGap;
        }
        //  Draw lines starting from bottom to right

        x = 0;
        y = height - lineGap;

        for (int i = 0; i < lines; i++)
        {
            g.drawLine(x, height, width, y);
            x += lineGap;
            y -= lineGap;
        }

        //  Draw lines starting from right to top

        //  Draw lines starting from top to left

    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("DrawSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new DrawSSCCE(15, 15) );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

答案 1 :(得分:0)

绘制此类图形的一种方法是将图形划分为多个象限。这是我想出的 GUI。

Curved Lines

此图是通过在矩形周围绘制线条来创建的。

我创建了一个 JFrame 和一个绘图 JPanel。我通过将水平宽度和垂直高度划分为相同数量的增量来创建绘图 JPanel。由于宽度大于高度,因此宽度增量大于高度增量。

我将绘图分成四部分,并分别处理每个季度的代码,使用越来越少的增量。一旦我让所有四个四分之一都工作,我将增量数量增加了四倍,并将宽度增量和高度增量除以四。

这是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

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

public class DrawOvalRectangle implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DrawOvalRectangle());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Curved Lines 2021");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(new DrawingPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        
        private int width, height, margin, increments, xIncrement, yIncrement;
        
        public DrawingPanel() {
            this.margin = 10;
            this.increments = 80;
            this.xIncrement = 8;
            this.yIncrement = 6;
            this.width = increments * xIncrement;
            this.height = increments * yIncrement;
            this.setPreferredSize(new Dimension(
                    width + margin + margin, height + margin + margin));
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            
            drawNorthwestQuadrant(g);
            drawNortheastQuadrant(g);
            drawSouthwestQuadrant(g);
            drawSoutheastQuadrant(g);
        }
        
        private void drawNorthwestQuadrant(Graphics g) {
            int x1 = margin;
            int y1 = height + margin;
            int x2 = margin;
            int y2 = margin;
            
            for (int index = 0; index < increments; index++) {
                g.drawLine(x1, y1, x2, y2);
                x2 += xIncrement;
                y1 -= yIncrement;
            }
        }

        private void drawNortheastQuadrant(Graphics g) {
            int x1 = margin;
            int y1 = margin;
            int x2 = width + margin;
            int y2 = margin;
            
            for (int index = 0; index < increments; index++) {
                g.drawLine(x1, y1, x2, y2);
                x1 += xIncrement;
                y2 += yIncrement;
            }
        }

        private void drawSouthwestQuadrant(Graphics g) {
            int x1 = margin;
            int y1 = height + margin;
            int x2 = margin;
            int y2 = margin;
            
            for (int index = 0; index < increments; index++) {
                g.drawLine(x1, y1, x2, y2);
                x1 += xIncrement;
                y2 += yIncrement;
            }
        }
        
        private void drawSoutheastQuadrant(Graphics g) {
            int x1 = margin;
            int y1 = height + margin;
            int x2 = width + margin;
            int y2 = height + margin;
            
            for (int index = 0; index < increments; index++) {
                g.drawLine(x1, y1, x2, y2);
                x1 += xIncrement;
                y2 -= yIncrement;
            }
        }
        
    }

}