我的图形没有显示在我的屏幕上
My Graphics are not being displayed on my screen
我正在尝试在 window 的中间以固定距离间隔打印出一条由小圆圈组成的水平线。我需要使用递归来做到这一点。当我调用递归方法来增加圆在屏幕上的位置以创建线条时,我使用了构造函数,但我的屏幕上没有打印出任何图形?
package weekFour;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.*;
@SuppressWarnings("serial")
public class Circle extends JPanel {
private static final int PREF_W = 200;
private static final int PREF_H = PREF_W;
private Color circleColor = Color.RED; //starting colour
private Color circleColor2 = Color.BLUE;
private Color squareColor = Color.GREEN;
private Color squareColor2 = Color.YELLOW;
private int circX = -15;
private int circY = circX;
private int circW = PREF_W - 2 * circX;
private int circH = PREF_H - 2 * circY;
private static int windowW = 2000;
private static int windowH = 1000;
public Circle() {
}
protected void paintComponent(Graphics g, int xval, int yval, int diameter) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //smoothes out edges
g2.setColor(circleColor);
g2.fillOval(xval, yval, diameter, diameter);
g2.setColor(Color.BLACK);
g2.drawOval(xval, yval, diameter, diameter);
paintComponent(g, circX + 25, 450, 12);
}
private static void createAndShowGui() { //code for GUI visuals
JFrame frame = new JFrame("MyTaskToo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Circle());
frame.setSize(windowW, windowH);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
感谢您的宝贵时间
首先查看 Painting in AWT and Swing and Performing Custom Painting 以了解有关绘画工作原理的更多详细信息。
您应该覆盖 paintComponent(Graphics g)
方法(注意,没有额外的参数),但在您这样做之前,您需要更改您的方法,因为您的递归方法调用将不起作用。
相反,您需要某种可以定期触发新绘图更新的后台线程
查看 Concurrency in Swing for the dangers inolved in doing this and How to use Swing Timers 以获得可能的解决方案
//需要在最后加一行重绘
圆()
{
重绘();
}
我正在尝试在 window 的中间以固定距离间隔打印出一条由小圆圈组成的水平线。我需要使用递归来做到这一点。当我调用递归方法来增加圆在屏幕上的位置以创建线条时,我使用了构造函数,但我的屏幕上没有打印出任何图形?
package weekFour;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.*;
@SuppressWarnings("serial")
public class Circle extends JPanel {
private static final int PREF_W = 200;
private static final int PREF_H = PREF_W;
private Color circleColor = Color.RED; //starting colour
private Color circleColor2 = Color.BLUE;
private Color squareColor = Color.GREEN;
private Color squareColor2 = Color.YELLOW;
private int circX = -15;
private int circY = circX;
private int circW = PREF_W - 2 * circX;
private int circH = PREF_H - 2 * circY;
private static int windowW = 2000;
private static int windowH = 1000;
public Circle() {
}
protected void paintComponent(Graphics g, int xval, int yval, int diameter) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //smoothes out edges
g2.setColor(circleColor);
g2.fillOval(xval, yval, diameter, diameter);
g2.setColor(Color.BLACK);
g2.drawOval(xval, yval, diameter, diameter);
paintComponent(g, circX + 25, 450, 12);
}
private static void createAndShowGui() { //code for GUI visuals
JFrame frame = new JFrame("MyTaskToo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Circle());
frame.setSize(windowW, windowH);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
感谢您的宝贵时间
首先查看 Painting in AWT and Swing and Performing Custom Painting 以了解有关绘画工作原理的更多详细信息。
您应该覆盖 paintComponent(Graphics g)
方法(注意,没有额外的参数),但在您这样做之前,您需要更改您的方法,因为您的递归方法调用将不起作用。
相反,您需要某种可以定期触发新绘图更新的后台线程
查看 Concurrency in Swing for the dangers inolved in doing this and How to use Swing Timers 以获得可能的解决方案
//需要在最后加一行重绘
圆()
{
重绘();
}