Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

java - hello, I am trying to draw a circle by using paintComponent

circle class

This is a simple circle class with only with a constructor.

public class Circle {

    private int radius;

    public Circle(int radius) {
        this.radius = radius;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }
    
    public double getArea() {
        return Math.PI * radius * radius;
    }

    @Override
    public String toString() {
        return "cicle [radius=" + radius + ", getRadius()=" + getRadius() + ", getArea()=" + getArea() + ", getClass()="
                + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
    }
}

Rock class

This is Rock class extends JPanel, that only have circle as variable. and overridden paintComponent.

import javax.swing.*;
import java.awt.*;

public class Rock extends JPanel {

    private Circle circle;
    
    public Rock(Circle circle) {
        this.circle=circle;
    }

    public Circle getCircle() {
        return circle;
    }

    public void setCircle(Circle circle) {
        this.circle = circle;
    }
    
    @Override
    public void paintComponent(Graphics j) {
        super.paintComponent(j);
        j.setColor(Color.black);
        int radius=new Circle(100).getRadius();
        j.fillOval(5, 5, radius, radius);
    }

    /*
     * public void paintcomponent(Graphics g) { super.paintComponent(g);
     * g.setColor(Color.black); int radius=new Circle(100).getRadius();
     * g.fillOval(0, 0, radius, radius); }
     */
}

Gui Stuff

This is the class where I was trying to construct everything. but when I added the Rock object to the layout, it would not appear.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class Gui extends JFrame implements ActionListener {
     JFrame topFrame = null; 
     GridBagLayout grid=new GridBagLayout();
     GridBagConstraints cons=new GridBagConstraints();
     Graphics j;
      Circle circle=new Circle(100);
      Rock rock;
    
    public Gui() {
          rock=new Rock(circle);
          RockGold h=new RockGold(circle);
          JLabel  lblResult=new JLabel(" Result ");

    this.setLayout(grid);
    cons=new GridBagConstraints();
    cons.gridx=1;
    cons.gridy=7;
    cons.insets=new Insets(10,10,10,0);
    
    this.add(rock,cons);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
    }
}

Main

This is the main with frames settings.

import javax.swing.JFrame;

public class main {

    public static void main(String[] args) {
    Gui gui=new Gui();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(900, 900);
    gui.setVisible(true);
    }
}
question from:https://stackoverflow.com/questions/66066540/hello-i-am-trying-to-draw-a-circle-by-using-paintcomponent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here are some tips on how to draw a circle.

  • create a class extending JPanel
  • create an instance of JFrame-- Do not subclass it
  • add the JPanel to the JFrame.
  • override paintComponent in your panel (like you have been doing).
  • simply call fillOval() or drawOval() with the appropriate arguments.

Note the use of rendering hints below.

public void paintComponent(Graphics g) {
   super.paintComponent(g);
   Graphics2D g2d = (Graphics2D) g;
   // the following will visually smooth the edges.
   g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON);
   g2d.setColor(Color.black);
   g2d.fillOval(5,5, 100,100);
}

Please check out painting in the Java Tutorials


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...