gpt4 book ai didi

java - 为什么我的 JComponent Paint(Graphics g) 不工作/不被调用?

转载 作者:行者123 更新时间:2023-11-30 06:08:27 25 4
gpt4 key购买 nike

我一直在尝试编写这个用点制作多边形的应用程序。当我第一次尝试运行它时,我认为我的点有问题。但是当我开始四处寻找问题时,我意识到 JComponent 中的绘制方法甚至没有被调用。我应该怎么做才能解决这个问题?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToggleButton;

public class PolygonBuilder extends JFrame{

final static int WIDTH = 1280;
final static int HEIGHT = 640;
public static int xPointPos, yPointPos;
public static void main(String[] args) {
new PolygonBuilder();
}
public PolygonBuilder() {
setTitle("Build a Polygon");
this.setSize(WIDTH,HEIGHT);
setResizable(false);
JLabel placeholder = new JLabel();
BuilderDrawingPanel BuilderPanel = new BuilderDrawingPanel();
this.add(BuilderPanel, BorderLayout.CENTER);

JToggleButton DotOnOff = new JToggleButton("Dot");
DotOnOff.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
if(DotOnOff.isSelected()) {
System.out.println("ON");
} else {
System.out.println("OFF");
}
}
});

DotOnOff.setBounds(100, 100, 90, 35);

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);
executor.scheduleAtFixedRate(new RepaintTheBoard(this), 0L, 20L, TimeUnit.MILLISECONDS);

addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent arg0) {}

@Override
public void mouseMoved(MouseEvent arg0) {
Point p = MouseInfo.getPointerInfo().getLocation();
xPointPos = (int) p.getX();
yPointPos = (int) p.getY();
System.out.println(xPointPos+","+yPointPos);
}
});

this.add(DotOnOff);
this.add(placeholder);
setVisible(true);
}
}
class RepaintTheBoard implements Runnable {
PolygonBuilder theBuilder;

public RepaintTheBoard(PolygonBuilder theBuilder) {
this.theBuilder = theBuilder;
}

@Override
public void run() {
theBuilder.repaint();
}
}

@SuppressWarnings("serial")
class BuilderDrawingPanel extends JComponent{
static Graphics2D graphicSettings;
static Shape drawLine;
public BuilderDrawingPanel() {
//stuff to draw at start
System.out.println("Drawing..");
drawLine = new Line2D.Float(480,480,0,0);
}
@Override
public void paint(Graphics g) {

graphicSettings = (Graphics2D)g;
System.out.println("Drawing..");
graphicSettings.setColor(Color.RED);
graphicSettings.fillRect(0, 0, getWidth(), getHeight());
graphicSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphicSettings.setPaint(Color.BLUE);
graphicSettings.draw(drawLine);
}
}

最佳答案

虽然 c0der 提供了一些有值(value)的信息和链接,但我认为应该在答案中解决其中的某些方面。

  • JFrame(的内容 Pane )的默认布局是 BorderLayout
  • 添加到没有约束的边框布局中的任何组件都默认位于CENTER
  • 边框布局的每个区域(布局约束)只能显示一个组件。

那么我们就来看看这部分代码:

this.add(DotOnOff);
this.add(placeholder);

JVM 将尝试将它们添加到已添加构建器绘图面板的内容 Pane 的中心。这就是为什么它甚至不会出现(或被绘制)。

其他提示:

  • 在任何 JComponent 中,重写自定义绘制的正确方法是 paintComponent(Graphics) 方法。
  • 任何重写的绘制方法都应立即调用 super 方法,以确保绘制背景、边框和其他“自动”元素。
  • Graphics 对象是 transient 的,因此不应存储为类的属性,更不应声明为static
  • 说到静态声明,它们(至少在GUI中)更多地是问题的根源而不是解决方案。不要将 GUI 属性声明为静态,除非您能解释这样做的原因。
  • 请学习常见的 Java 命名法(命名约定 - 例如 EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT)并一致地使用它。

更新

这是问题中看到的来源,解决了最直接的问题。它并没有改变太多推荐的其他内容,也没有触及我在使用代码本身时看到的其他问题。查看评论以了解为使其发挥作用而进行的更改。

这就是它的样子,经过更改:

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.concurrent.*;
import javax.swing.*;

public class PolygonBuilder extends JFrame{

final static int WIDTH = 640;
final static int HEIGHT = 320;
public static int xPointPos, yPointPos;
public static void main(String[] args) {
new PolygonBuilder();
}
public PolygonBuilder() {
setTitle("Build a Polygon");
// let's be nice to the user..
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this is almost certainly not the correct size,
// but in itself could deserve a question
this.setSize(WIDTH,HEIGHT);
setResizable(false);
JLabel placeholder = new JLabel("Label!");
BuilderDrawingPanel BuilderPanel = new BuilderDrawingPanel();
this.add(BuilderPanel, BorderLayout.CENTER);

JToggleButton DotOnOff = new JToggleButton("Dot");
DotOnOff.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
if(DotOnOff.isSelected()) {
System.out.println("ON");
} else {
System.out.println("OFF");
}
}
});

DotOnOff.setBounds(100, 100, 90, 35);

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);
executor.scheduleAtFixedRate(new RepaintTheBoard(this), 0L, 20L, TimeUnit.MILLISECONDS);

addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent arg0) {}

@Override
public void mouseMoved(MouseEvent arg0) {
Point p = MouseInfo.getPointerInfo().getLocation();
xPointPos = (int) p.getX();
yPointPos = (int) p.getY();
System.out.println(xPointPos+","+yPointPos);
}
});

// create a new panel for these buttons..
//this.add(DotOnOff);
//this.add(placeholder);
JPanel buttonPanel = new JPanel(); // default flow layout
buttonPanel.add(DotOnOff);
buttonPanel.add(placeholder);
// now add that panel above the builder panel
this.add(buttonPanel, BorderLayout.PAGE_START);
setVisible(true);
}
}
class RepaintTheBoard implements Runnable {
PolygonBuilder theBuilder;

public RepaintTheBoard(PolygonBuilder theBuilder) {
this.theBuilder = theBuilder;
}

@Override
public void run() {
theBuilder.repaint();
}
}

@SuppressWarnings("serial")
class BuilderDrawingPanel extends JComponent{
static Graphics2D graphicSettings;
static Shape drawLine;
public BuilderDrawingPanel() {
//stuff to draw at start
System.out.println("Drawing..");
drawLine = new Line2D.Float(480,480,0,0);
}
@Override
public void paint(Graphics g) {

graphicSettings = (Graphics2D)g;
System.out.println("Drawing..");
graphicSettings.setColor(Color.RED);
graphicSettings.fillRect(0, 0, getWidth(), getHeight());
graphicSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphicSettings.setPaint(Color.BLUE);
graphicSettings.draw(drawLine);
}
}

关于java - 为什么我的 JComponent Paint(Graphics g) 不工作/不被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50773600/

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