【发布时间】:2015-11-18 11:40:41
【问题描述】:
所以我正在制作一些简单的游戏,我尝试在带有图像图标的标签上绘制矩形,但是当我尝试在框架上绘制一些东西时没有任何效果,我尝试使用图像图标删除标签,但我仍然无法绘制框架上的任何东西。 这是我的代码
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一次您的固定点之一)以保留油漆链
标签: java image swing graphics 2d