2010-11-20 32 views
0

Salut J'ai fait un simulateur d'incendie avec JTextArea http://xieu901.webs.com/fire.jarjava, peinture JPanel, qui est dans un autre JPanel

maintenant je veux le faire avec JPanel ou toile au lieu de JTextArea (Je veux juste me mettre au défi ^^) Je réussi à dessiner une image dans une seule JPanel dans Jframe , mais je ne peux pas tirer quoi que ce soit dans un JPanel qui est dans un autre JPanel dans Jframe

sont là un moyen d'utiliser la méthode de peinture sans étend JComponent? et je ne sais pas comment créer un nouveau objet Graphics

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

avec le code ci-dessus, je suis un JPanel normal (qui devrait être noir, donc je pense que la méthode de peinture était pas utilisé)

et ici était mon le code qui est JPanel en JFrame, avec elle, il a travaillé

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

Répondre

2

Non, les graphiques de l'interface graphique est créé par swing. Cet exemple fonctionne et peut vous donner un indice:

public static void main(String... args) { 
    JFrame frame = new JFrame("Test"); 

    JPanel outer = new JPanel(new GridLayout(1, 1)); 
    JPanel inner = new JPanel(new GridLayout(1, 1)); 
    MyComponent component = new MyComponent(); 

    frame.add(outer, BorderLayout.CENTER); 
    outer.add(inner); 
    inner.add(component); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 300); 
    frame.setVisible(true); 
} 

static class MyComponent extends JComponent { 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     g.setColor(Color.BLACK); 
     Rectangle r = getBounds(); 
     g.drawRect(r.x + 10, r.y + 10, r.width - 20, r.height - 20); 
    } 
} 
+0

+1 « Swing les programmes devraient surcharger 'paintComponent()' au lieu de surcharger 'paint()', "http://java.sun.com/products/jfc/tsc/articles/painting/index.html – trashgod

0

grâce dacwe, votre code m'a aidé, maintenant je peux commencer le vrai travail ^^

alt text

package nad; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.Image; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

import jpanel.gui; 

import net.miginfocom.swing.MigLayout; 

    class Componentprobe extends JComponent{ 

    ImageIcon fireicon = loadImage("regen.png"); 
    Image fire= fireicon.getImage(); 

    private ImageIcon loadImage(String s) { 
      java.net.URL imgURL = gui.class.getResource(s); 
      return new ImageIcon(imgURL); 
    } 

    protected void paintComponent(Graphics g) { 
//  super.paintComponent(g); 
// 
//  g.setColor(Color.BLACK); 
//  Rectangle r = getBounds(); 
//  g.drawRect(r.x + 10, r.y + 10, r.width - 20, r.height - 20); 

     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()); 
     for (int i = 0; i < 10; i++) { 
      for (int j = 0; j < 10; j++) { 
       g2d.drawImage(fire,1+i*50,1+j*50,50,50,null); 
      } 
     } 

    } 
    public static void main(String[] args) { 
     JButton button1= new JButton("OK"); 
     JButton button2= new JButton("reset"); 
     JButton button3= new JButton("update"); 

     MigLayout layout= new MigLayout("fillx,filly", "[70%,fill][30%,fill]",""); 
     Componentprobe component = new Componentprobe(); 
     JPanel panel = new JPanel(new GridLayout(1, 1)); //Miglayout or null layout wont work 
     JPanel main= new JPanel(layout); 
     JFrame win = new JFrame("component probe"); 


     win.setVisible(true); 
     win.setSize(400, 400); 

     win.add(main); 
     main.add(panel,"spany 3,aligny top,align left"); 
     panel.setPreferredSize(panel.getMaximumSize());//wichtig 
     panel.add(component); 


     main.add(button1,"wrap,aligny top"); 
     main.add(button2,"wrap,aligny top"); 
     main.add(button3,"aligny t"); 


     win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     win.setSize(400, 300); 
     win.setVisible(true); 
    } 

} 
+0

Heureux d'avoir aidé! :) – dacwe