Java:无法在框架上绘制二维图形

标签 java image swing graphics 2d

所以我正在制作一些简单的游戏,我尝试在带有图像图标的标签上绘制矩形,但是当我尝试在框架上绘制一些东西时没有任何效果,我尝试删除带有图像图标的标签,但我仍然无法绘制框架上的任何东西。 这是我的代码

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

import java.awt.geom.*;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class Game extends JFrame{
    JButton but1=new JButton("MakeMoney");
    JButton button1=new JButton("Current money");
    int money=0;
    double currentMoney;
    String moneyString="";

    public static void main(String[] args){
        new Game(); 
    }

    public Game(){

        try {
        this.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/TPC/workspace/ProjectMoney/Resources/backgroundForApp.png")))));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        this.setLayout(new FlowLayout());

        button1.setContentAreaFilled(false);

        ListenForButton lforButton=new ListenForButton();
        but1.addActionListener(lforButton);


        JPanel thePanel=new JPanel();
        thePanel.add(button1);
        thePanel.add(but1);

        this.setSize(1042, 617);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        this.setTitle("Project Money");
        this.add(thePanel);
        this.add(new DrawStuff(), BorderLayout.CENTER);
        this.setResizable(true);
        this.setVisible(true);

        Sound sound1=new Sound();
        String sound = "file:C:/Users/TPC/Downloads/sound.wav";
        sound1.playMusic(sound);

    }   

    private class ListenForButton implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==but1){
                money+=10;
                moneyString=Integer.toString(money);
                button1.setText("$"+moneyString);
            }
        }
    }
    private class DrawStuff extends JComponent{
        public void paint(Graphics g){
            Graphics2D graph2 = (Graphics2D)g;
            graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Shape drawLine = new Line2D.Float(20, 90, 55, 250);

            Shape drawArc2D = new Arc2D.Double(5, 150, 100, 100, 45, 180, Arc2D.OPEN);

            Shape drawArc2D2 = new Arc2D.Double(5, 200, 100, 100, 45, 45, Arc2D.CHORD);

            Shape drawArc2D3 = new Arc2D.Double(5, 250, 100, 100, 45, 45, Arc2D.PIE);

            Shape drawEllipse = new Ellipse2D.Float(10, 10, 100, 100);

            Shape drawRoundRec = new RoundRectangle2D.Double(25, 25, 50, 50, 45, 45);
            CubicCurve2D cubicCurve = new CubicCurve2D.Double();

            cubicCurve.setCurve(110, 50, 300,
                    200, 200, 200, 90, 263);

            Shape drawRect = new Rectangle2D.Float(300, 300, 150, 100);

            Shape drawQuadCurve = new QuadCurve2D.Float(300, 100, 400, 200, 150, 300);

            Shape drawTransRect = new Rectangle2D.Double(300, 300, 75, 50);

            graph2.setPaint(Color.BLACK);

            graph2.draw(drawLine);

            graph2.draw(drawArc2D);

            graph2.draw(drawArc2D2);

            graph2.draw(drawArc2D3);

            graph2.draw(drawEllipse);

            graph2.setColor(Color.GREEN);

            graph2.fill(drawRoundRec);
            graph2.fill(drawRect);
            graph2.setPaint(Color.BLACK);
            graph2.draw(cubicCurve);
            graph2.draw(drawRect);
            graph2.draw(drawQuadCurve);
            graph2.fill(new Rectangle2D.Float(10, 10, 150, 100));
            graph2.fill(drawTransRect);
        }
    }
}

最佳答案

  1. 使用paintComponent代替paint
  2. 调用super.paint(或super.paintComponent一旦你的固定点)来保存油漆链
  3. 不要将 JLabel 用作背景组件,它不会考虑组件的首选大小,只会考虑图标的大小 + 标签的文本。
  4. 您正在为您的 contentPane 分配一个 FlowLayout,但没有为您的 DrawStuff 面板提供任何大小调整提示,这意味着当它布局后,它将给出 0x0 的大小。尝试使用 BorderLayout 代替

例如……

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Game extends JFrame {

    JButton but1 = new JButton("MakeMoney");
    JButton button1 = new JButton("Current money");
    int money = 0;
    double currentMoney;
    String moneyString = "";

    public static void main(String[] args) {
        new Game();
    }

    public Game() {

        try {
            this.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/TPC/workspace/ProjectMoney/Resources/backgroundForApp.png")))));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        this.setLayout(new BorderLayout());

        button1.setContentAreaFilled(false);

        ListenForButton lforButton = new ListenForButton();
        but1.addActionListener(lforButton);

        JPanel thePanel = new JPanel();
        thePanel.add(button1);
        thePanel.add(but1);

        this.setSize(1042, 617);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Project Money");
        this.add(thePanel, BorderLayout.NORTH);
        this.add(new DrawStuff(), BorderLayout.CENTER);
        this.setResizable(true);
        this.setVisible(true);

        Sound sound1 = new Sound();
        String sound = "file:C:/Users/TPC/Downloads/sound.wav";
        sound1.playMusic(sound);

    }

    private class ListenForButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == but1) {
                money += 10;
                moneyString = Integer.toString(money);
                button1.setText("$" + moneyString);
            }
        }
    }

    private class DrawStuff extends JComponent {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D graph2 = (Graphics2D) g;
            graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Shape drawLine = new Line2D.Float(20, 90, 55, 250);

            Shape drawArc2D = new Arc2D.Double(5, 150, 100, 100, 45, 180, Arc2D.OPEN);

            Shape drawArc2D2 = new Arc2D.Double(5, 200, 100, 100, 45, 45, Arc2D.CHORD);

            Shape drawArc2D3 = new Arc2D.Double(5, 250, 100, 100, 45, 45, Arc2D.PIE);

            Shape drawEllipse = new Ellipse2D.Float(10, 10, 100, 100);

            Shape drawRoundRec = new RoundRectangle2D.Double(25, 25, 50, 50, 45, 45);
            CubicCurve2D cubicCurve = new CubicCurve2D.Double();

            cubicCurve.setCurve(110, 50, 300,
                    200, 200, 200, 90, 263);

            Shape drawRect = new Rectangle2D.Float(300, 300, 150, 100);

            Shape drawQuadCurve = new QuadCurve2D.Float(300, 100, 400, 200, 150, 300);

            Shape drawTransRect = new Rectangle2D.Double(300, 300, 75, 50);

            graph2.setPaint(Color.BLACK);

            graph2.draw(drawLine);

            graph2.draw(drawArc2D);

            graph2.draw(drawArc2D2);

            graph2.draw(drawArc2D3);

            graph2.draw(drawEllipse);

            graph2.setColor(Color.GREEN);

            graph2.fill(drawRoundRec);
            graph2.fill(drawRect);
            graph2.setPaint(Color.BLACK);
            graph2.draw(cubicCurve);
            graph2.draw(drawRect);
            graph2.draw(drawQuadCurve);
            graph2.fill(new Rectangle2D.Float(10, 10, 150, 100));
            graph2.fill(drawTransRect);
        }
    }
}

学习从本地上下文而不是绝对上下文加载资源,从长远来看,这将使您的生活更简单。类似于 Retrieving Resources

另外,看看 Painting in AWT and SwingPerforming Custom Painting有关绘画如何工作的更多详细信息

关于Java:无法在框架上绘制二维图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32192634/

相关文章:

java - 如何在随机坐标上绘制一个随机大小的圆,使圆完全可见?

java - Android API 中存在多少核心 Java API

java - 正则表达式从 URL 中提取 ip 地址

java - 如何知道列表的数据类型

image - 删除图像文件 yii

android - 如果 Android/iOS 缩放它们,为什么不同密度桶的图像尺寸不同?

html - 使较小的图像覆盖较大的 div 并保持宽高比

java - 如何在 JTable 中创建一行包含 367 个单元格的行,其中只有第一个单元格有文本,其余单元格有颜色

java - 使用 Java 和 Corba 识别点对点文件共享中的客户端。并在 2 个客户端之间构建套接字套接字

java - 如何自动点击JOptionPane的OK