尝试使用JPanel绘制线条 [英] Trying to draw lines with JPanel

查看:89
本文介绍了尝试使用JPanel绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 JPanel 绘制线条,而且我已经打了一些墙。我可以将两面朝下,但一旦从x线中减去它就会出错。

I am trying to draw lines using JPanel and I have hit somewhat of a wall. I can get two sides down but once it comes to subtracting from the x cord it all goes wrong.

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减去时,我只得到一条线穿过整个框。

I have a the lines in the bottom left and the upper right but when I try to subtract from x I just get lines going a crossed the whole box.

推荐答案

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

When doing custom painting you should override the getPreferredSize() method so the panel can be displayed at its preferred size.

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

When you draw the lines two variable are the same and two variables differ. Use the width/height variable when appropriate instead of hardcoding a number. In the example below I did the left and bottom sides. The bottom side shows how to subtract. I'll let you figure out the pattern for the other two side.

另外,我让面板更加动态,所以很容易配置数量你想画的线条和线条之间的间隙。

Also, I made the panel a little more dynamic so it will be easy to configure the number of lines you want painted and the gap between the lines.

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();
            }
        });
    }
}

这篇关于尝试使用JPanel绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆