简体   繁体   中英

Java:Can't draw 2D graphics on frame

So i am making some simple game,and i tried to draw rectangle on label with image icon,but when i try to draw something on frame nothing works,i tried removing label with image icon,but still i can't draw anything on frame. Here is my code

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. Use paintComponent instead of paint
  2. Call super.paint (or super.paintComponent once your fixed point one) to preserve the paint chain
  3. Don't use JLabel as a background component, it won't respect the preferred size of your components, only the size of the icon + text of the label.
  4. You're assigning a FlowLayout to your contentPane , but aren't providing any sizing hints for your DrawStuff panel, meaning when it's laid out, it will given the size of 0x0 . Try using a BorderLayout instead

For example...

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

Learn to load resources from within a local context rather then an absolute context, it will make your life simpler in the long run. Something like Retrieving Resources

Also, have a look at Painting in AWT and Swing and Performing Custom Painting for more details about how painting works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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