gpt4 book ai didi

java - 尝试用 JPanel 画线

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:49:22 25 4
gpt4 key购买 nike

我正在尝试使用 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++;

}
}
}

这是我的运行者类(class)

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 中减去时,我得到的线穿过了整个框。

最佳答案

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

当你画线时,两个变量相同,两个变量不同。在适当的时候使用宽度/高度变量而不是硬编码数字。在下面的示例中,我做了左侧和底部。底部显示了如何减去。我会让你找出另外两侧的模式。

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

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

关于java - 尝试用 JPanel 画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16206417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com