gpt4 book ai didi

java - 关于在java中绘制多边形

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:08:44 27 4
gpt4 key购买 nike

您好,我正在尝试提高 java2D 方面的知识,首先我正在处理绘制多边形。但是,我看不到框架上的多边形。我阅读了一些教程和示例,但正如我所说,我遇到了问题。这是绘制多边形的示例代码;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.JFrame;

public class jRisk extends JFrame {


private JFrame mainMap;
private Polygon poly;

public jRisk(){

initComponents();

}

private void initComponents(){

mainMap = new JFrame();
mainMap.setSize(800, 600);
mainMap.setResizable(false);

mainMap.setVisible(true);
mainMap.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

int xPoly[] = {150,250,325,375,450,275,100};
int yPoly[] = {150,100,125,225,250,375,300};

poly = new Polygon(xPoly, yPoly, xPoly.length);

}

protected void paintComponent(Graphics g){

super.paintComponents(g);

g.setColor(Color.BLUE);
g.drawPolygon(poly);

}

/**
* @param args
*/
public static void main(String[] args) {

new jRisk();

}

}

最佳答案

JFrame 没有 paintComponent(Graphics g)方法。添加@Override annotation你会得到一个编译时错误。

1) 使用 JPanel 并覆盖 paintComponent(您可以通过 添加 JPanelJFrame >JFrame#add(..))

2) 覆盖 getPreferredSize() 以返回适合您在 Graphics 对象上绘图的正确 Dimension,否则它们将不会被视为 JPanel没有组件的大小是 0,0

3) 不要在 JFrame 上调用 setSize...而是使用正确的 LayoutManager 和/或覆盖 getPrefferedSize() 并在添加所有组件之后但在将其设置为可见之前调用 JFrame 上的 pack()

4) 阅读 Concurrency in Swing具体关于Event Dispatch Thread

5) watch 类命名方案应以大写字母开头,此后每个新词的首字母均应大写

6) 您还扩展JFrame 并有一个变量JFrame 吗?去掉 extend JFrame 并保留 JFrame 变量,因为我们不想要 2 个 JFrame 并且扩展 JFrame< 不是好的做法 除非添加功能

这是您的代码并进行了上述修复(请原谅图片质量但必须调整大小或将变为 800x600):

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JRisk {

private JFrame mainMap;
private Polygon poly;

public JRisk() {

initComponents();

}

private void initComponents() {

mainMap = new JFrame();
mainMap.setResizable(false);

mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

int xPoly[] = {150, 250, 325, 375, 450, 275, 100};
int yPoly[] = {150, 100, 125, 225, 250, 375, 300};

poly = new Polygon(xPoly, yPoly, xPoly.length);
JPanel p = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawPolygon(poly);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
};
mainMap.add(p);
mainMap.pack();
mainMap.setVisible(true);

}

/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JRisk();
}
});
}
}

根据您的评论:

i am preparing a map which includes lots of polygons and yesterday i used a JPanel over a JFrame and i tried to check if mouse was inside of the polygon with MouseListener. later i saw that mouseListener gave false responds (like mouse is not inside of the polygon but it acts like it was inside the polygon). so i deleted the JPanel and then it worked

这里是使用 MouseAdapter 和覆盖 mouseClicked 来检查点击是否在多边形内的更新代码。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JRisk {

private JFrame mainMap;
private Polygon poly;

public JRisk() {
initComponents();
}

private void initComponents() {

mainMap = new JFrame();
mainMap.setResizable(false);

mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

int xPoly[] = {150, 250, 325, 375, 450, 275, 100};
int yPoly[] = {150, 100, 125, 225, 250, 375, 300};

poly = new Polygon(xPoly, yPoly, xPoly.length);

JPanel p = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawPolygon(poly);
}

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

MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
super.mouseClicked(me);

if (poly.contains(me.getPoint())) {
System.out.println("Clicked polygon");
}

}
};
p.addMouseListener(ma);//add listener to panel
mainMap.add(p);

mainMap.pack();
mainMap.setVisible(true);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JRisk();
}
});
}
}

关于java - 关于在java中绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15188238/

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