如何显示其他班级的图片

时间:2019-04-16 21:12:52

标签: java swing graphics awt

我正在尝试在主类的JPanel上显示来自单独类的图形。

主要课程是mytest,单独的课程是Ball。球具有绘画成分方法,并且仅绘制一个彩色圆圈。在mytest中,我实例化了一个球并将其添加到JPanel(dp):dp.add(ball)。很简单,但是我得到的只是白色面板背景,没有绘制球。

这是最神秘的代码:

package myStuff;

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

public class mytest {
    private JFrame frame=new JFrame();
    private JPanel dp = new JPanel();


    public static void main(String[] args) {
        mytest gui = new mytest();
        gui.go();
    }
    public void go() {      
        frame.setTitle("Test");
        frame.setSize(1000,600);        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel dp=new JPanel();
        dp.setBackground(Color.WHITE);
        Ball ball = new Ball(dp.getWidth(),dp.getHeight());
        dp.add(ball);
        frame.add(dp);
        frame.setVisible(true);
    }
}

这是班级代码:

package myStuff;

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

public class Ball extends JComponent{

    private int Width; 
    private int Height; 
    public Ball (int width, int height ) {
        Width=width;
        Height=height;      
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        super.paintComponent(g2d);
        g2d.setColor(Color.RED);
        g2d.fillOval(Width/2,Height/2,40,40);
        System.out.println("Doing graphics....");
    }
}

一个红色的球应该出现在dp面板上。我得到的只是面板背景,没有球。我知道它正在尝试,因为“执行图形”打印了两次。

3 个答案:

答案 0 :(得分:1)

Here is a working example.

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

public class Mytest {
   private JFrame frame = new JFrame();

   public static void main(String[] args) {
      Mytest gui = new Mytest();
      SwingUtilities.invokeLater(() -> gui.go());
   }

   public void go() {
      frame.setTitle("Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel dp = new JPanel();
      dp.setPreferredSize(new Dimension(500, 500));
      dp.setBackground(Color.WHITE);
      Ball ball = new Ball(150, 150);
      dp.add(ball);
      frame.add(dp);
      frame.pack(); // invokes layout and sizes components
      frame.setLocationRelativeTo(null); // centers on screen
      frame.setVisible(true);
   }

}

class Ball extends JComponent {

   private int width;
   private int height;

   // A ball should probably only have a "diameter"
   public Ball(int width, int height) {
      this.width = width;
      this.height = height;
      setPreferredSize(new Dimension(width, height));
   }

   @Override
   public void paintComponent(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
      super.paintComponent(g2d);
      g2d.setColor(Color.RED);
      // smooths out the graphics
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2d.fillOval(0, 0, width, height);
      System.out.println("Doing graphics....");
   }
}

The two biggest suggestions are to:

  1. Ensure when you change a Swing component you do so on the Event Dispatch Thread.
  2. And use the anti aliasing to make your drawing look smoother (note this is optional and it can add extra processing overhead.)

The reason no red ball was drawn (or only 1/4 of one) was because you changed the location of where to draw it within the Component window. You tried to draw it at width/2 and height/2 which was the center of the Component. It should have been at 0,0 for normal rendering.

Also read about painting in the The Java Tutorials 1

答案 1 :(得分:0)

您正在设置框架的尺寸,但是面板的尺寸为零。您应该设置面板的首选大小,而不是框架的大小。然后,将面板的首选大小传递给Ball构造函数,并在使框架可见之前打包框架。

答案 2 :(得分:0)

Set the panel size before the line,

Ball ball = new Ball(dp.getWidth(),dp.getHeight());

Then add this code

setPreferredSize(new Dimension(Width, Height));

at the end of the "Ball" Constructor.

See this stack question for more details.