java,paint Jpanel,它在另一个JPanel里面
你好 我用 JTextArea 做了一个火灾模拟器 http://xieu901.webs.com/fire.jar
现在我想用 JPanel 来制作它或者Canvas而不是JTextarea(只是想挑战自己^^) 我设法在 Jframe 中的唯一 JPanel 中绘制图像 但我无法在 Jframe 中另一个 JPanel 中的 Jpanel 中绘制任何内容,
有没有办法在不扩展 JComponent 的情况下使用 Paint 方法? 我不知道如何
public class gui extends JComponent {
//create gui elements
MigLayout layout= new MigLayout("fillx,filly", "[70%,fill][30%,fill]","");
JLabel status = new JLabel("status");
JTextField sizeoffield = new JTextField();
JButton ok= new JButton("Start");
JButton reset= new JButton("Reset");
JButton update= new JButton("Update");
JPanel mainPanel = new JPanel(layout);
JPanel panel = new JPanel();
JFrame win = new JFrame("my win");
//constructor = create gui
gui(){
win.setVisible(true);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setBounds(1,1,800,600);
panel.setPreferredSize(panel.getMaximumSize());//wichtig
win.add(mainPanel);
mainPanel.add(panel,"spany 4,align left,aligny top");
mainPanel.add(sizeoffield,"wrap");
mainPanel.add(ok,"wrap,aligny t");
mainPanel.add(reset,"wrap,aligny t");
mainPanel.add(update,"wrap,aligny t");
mainPanel.add(status);
panel.addMouseListener(mouse);
ok.addActionListener(listener);
reset.addActionListener(listener);
update.addActionListener(listener);
}
/*******a long code of mouse/actionlistener and other methods was cut here *******/
//load imageicon to convert to image later
private ImageIcon loadImage(String s) {
java.net.URL imgURL = gui.class.getResource(s);
return new ImageIcon(imgURL);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawString("Java 2D", 50, 50);
g2d.drawRoundRect(1, 1, 100, 100, 100, 100);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.drawImage(fire,1,1,50,50,null);
}
@Override
public void repaint() {
super.repaint();
}
public static void main(String[] args) {
new gui();
}
}
使用上面的代码创建一个新的 Graphics 对象,我得到了一个普通的 JPanel (应该是黑色的,所以我认为没有使用绘制方法)
,这是我的代码,它是 JFrame 中的 JPanel ,它可以工作
package jpanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Panel extends JComponent{ //class Panel
ImageIcon fireicon = loadImage("regen.png");
ImageIcon normalicon = loadImage("regen.png");
ImageIcon regenicon = loadImage("regen.png");
Image fire= fireicon.getImage();
Image normal= normalicon.getImage();
Image regen= regenicon.getImage();
private ImageIcon loadImage(String s) {
java.net.URL imgURL = gui.class.getResource(s);
return new ImageIcon(imgURL);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawString("Java 2D", 50, 50);
g2d.drawRoundRect(1, 1, 100, 100, 100, 100);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.drawImage(fire,1,1,50,50,null);
}
// @Override
// public void repaint() {
// super.repaint();
// }
public static void main(String[] args) {
JFrame win = new JFrame();
win.add(new Panel());
win.setVisible(true);
win.setSize(400, 400);
}
}
Hi
I made a Fire Simulator with JTextArea
http://xieu901.webs.com/fire.jar
now I want to make it with JPanel or Canvas instead of JTextarea (just want to challenge myself ^^ )
I managed to draw an image in a only JPanel in Jframe
but I cant draw anything in a Jpanel which is in another JPanel in Jframe
are there any way to use the paint method without extends JComponent ?
and I dont know how to create a new Graphics object
public class gui extends JComponent {
//create gui elements
MigLayout layout= new MigLayout("fillx,filly", "[70%,fill][30%,fill]","");
JLabel status = new JLabel("status");
JTextField sizeoffield = new JTextField();
JButton ok= new JButton("Start");
JButton reset= new JButton("Reset");
JButton update= new JButton("Update");
JPanel mainPanel = new JPanel(layout);
JPanel panel = new JPanel();
JFrame win = new JFrame("my win");
//constructor = create gui
gui(){
win.setVisible(true);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setBounds(1,1,800,600);
panel.setPreferredSize(panel.getMaximumSize());//wichtig
win.add(mainPanel);
mainPanel.add(panel,"spany 4,align left,aligny top");
mainPanel.add(sizeoffield,"wrap");
mainPanel.add(ok,"wrap,aligny t");
mainPanel.add(reset,"wrap,aligny t");
mainPanel.add(update,"wrap,aligny t");
mainPanel.add(status);
panel.addMouseListener(mouse);
ok.addActionListener(listener);
reset.addActionListener(listener);
update.addActionListener(listener);
}
/*******a long code of mouse/actionlistener and other methods was cut here *******/
//load imageicon to convert to image later
private ImageIcon loadImage(String s) {
java.net.URL imgURL = gui.class.getResource(s);
return new ImageIcon(imgURL);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawString("Java 2D", 50, 50);
g2d.drawRoundRect(1, 1, 100, 100, 100, 100);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.drawImage(fire,1,1,50,50,null);
}
@Override
public void repaint() {
super.repaint();
}
public static void main(String[] args) {
new gui();
}
}
with above code I got a normal JPanel (which should be black, so I think the paint method wasnt used)
and here was my code which is JPanel in JFrame , with it it worked
package jpanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Panel extends JComponent{ //class Panel
ImageIcon fireicon = loadImage("regen.png");
ImageIcon normalicon = loadImage("regen.png");
ImageIcon regenicon = loadImage("regen.png");
Image fire= fireicon.getImage();
Image normal= normalicon.getImage();
Image regen= regenicon.getImage();
private ImageIcon loadImage(String s) {
java.net.URL imgURL = gui.class.getResource(s);
return new ImageIcon(imgURL);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawString("Java 2D", 50, 50);
g2d.drawRoundRect(1, 1, 100, 100, 100, 100);
g2d.setColor(Color.black);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
g2d.drawImage(fire,1,1,50,50,null);
}
// @Override
// public void repaint() {
// super.repaint();
// }
public static void main(String[] args) {
JFrame win = new JFrame();
win.add(new Panel());
win.setVisible(true);
win.setSize(400, 400);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,GUI 的图形是由 swing 创建的。这个例子有效并且可能会给你一个提示:
No, the graphics for the GUI is created by swing. This example works and may give you a hint:
谢谢达克维,
你的代码帮助了我,现在我可以开始真正的工作了 ^^
thanks dacwe,
your code helped me, now I can start the real work ^^