简体   繁体   English

屏幕上未显示我的图形

[英]My Graphics are not being displayed on my screen

I am trying to print out a horizontal line of small circles at regular distance intervals across the middle of my window. 我正在尝试在窗口中间以规则的距离间隔打印一条水平的小圆圈线。 It is required of me to do this using recursion. 我需要使用递归来做到这一点。 I use the constructors when I call my recursive method to increment the position of the circle on the screen to create the line but no graphics are being printed out on my screen? 当我调用递归方法以增加圆在屏幕上的位置以创建线时使用构造函数,但我的屏幕上没有打印出图形吗?

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

Thanks for your time 谢谢你的时间

Start by taking a look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works. 首先查看AWT中的绘画和Swing执行自定义绘画,以获取有关绘画工作原理的更多详细信息。

You should be overriding the paintComponent(Graphics g) methods (notice, no extra parameters), but before you do this, you will want to change your approach as your recursive method calls won't work. 您应该重写paintComponent(Graphics g)方法(注意,没有额外的参数),但是在执行此操作之前,您将需要更改方法,因为递归方法调用将无法进行。

Instead, you'll need some kind of background thread that can trigger a new paint update at a regular interval 取而代之的是,您需要某种背景线程,该线程可以定期触发新的绘画更新

Take a look at Concurrency in Swing for the dangers inolved in doing this and How to use Swing Timers for a possible solution 查看Swing中的并发,了解这样做所带来的危险以及如何使用Swing计时器作为可能的解决方案

//you need to add a line in the end to repaint. //您需要在最后添加一行以重绘。
Circle() 圈()

{ {

repaint(); repaint();

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM