Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - how to add a png file to JPanel
    text
    copied!<p>This Friday I need to hand in my Java project – I've only been programming for about two weeks now – and I really want to add a PNG file to my JPanel. Below you find my code, the name of the image is <strong>java.png</strong>, I put it in the same folder as my src files. I just can't find a good tutorial on how to do this. Please help me!<br> <em>Note:</em> the image should be inserted in the <code>booOpAfbeam</code> structure almost at the bottom of the code, and also sorry for this unorganized message, I have no clue how to post my code decently.</p> <pre><code> import java.awt.Color; import java.awt.Graphics; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.ImageObserver; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import java.awt.Image; import java.awt.Dimension; import javax.swing.*; public class Project extends JPanel { private JLabel invoerLabel, invoerKleur, invoerKleur2, invoerBril, invoerBril2; private JTextField invoerAantal, invoerK, invoerB; private int intAantal; private String strKleur=""; private String strBril=""; private JButton knop, knopBril, btnOpAf, btnOpAfbeam; private Boolean booOpAf = false, booOpAfbeam = false; private Image image1; public Project(){ setLayout(null); invoerLabel = new JLabel("Voer aantal stoelen in: "); invoerAantal = new JTextField (10); invoerKleur = new JLabel("Voer kleur stoel in: "); invoerKleur2 = new JLabel("(blauw, rood, groen, paars, roos, zwart, geel, oranje)"); invoerK = new JTextField (10); invoerBril = new JLabel("Voer kleur bril in: "); invoerBril2 = new JLabel("(blauw, rood, groen, paars, roos, zwart, geel, oranje)"); invoerB = new JTextField (10); knopBril = new JButton ("ok"); knop = new JButton("ok"); btnOpAf = new JButton ("Wijs/Wijs niet"); btnOpAfbeam = new JButton ("aan/uit"); knop.addActionListener (new InvoervakHandler()); knopBril.addActionListener(new InvoervakHandler2()); btnOpAf.addActionListener(new aanuit()); btnOpAfbeam.addActionListener(new beam()); invoerLabel.setBounds(1000, 10, 150, 20); invoerAantal.setBounds(1150, 10, 60, 20); invoerKleur.setBounds(1000, 70, 120, 20); invoerKleur2.setBounds(985, 90, 300, 20); invoerK.setBounds(1150, 70, 60, 20); knop.setBounds(1155, 120, 50, 20); invoerBril.setBounds(1000, 170, 150, 20); invoerBril2.setBounds(985, 195, 300, 20); invoerB.setBounds(1150, 170, 60, 20); knopBril.setBounds(1155, 220, 50, 20); btnOpAf.setBounds(1000,275,115,20); btnOpAfbeam.setBounds(365, 26, 75, 20); add(invoerLabel); add(invoerAantal); add(invoerKleur); add(invoerKleur2); add(invoerK); add(invoerBril); add(invoerBril2); add(invoerB); add(knop); add(knopBril); add(btnOpAf); add(btnOpAfbeam); } public void paintComponent(Graphics g) { super.paintComponent(g); ImageIcon i = new ImageIcon("java.png"); image1 = i.getImage(); g.drawImage(image1, 0, 0, null); ImageIcon("java.png"); //labels// invoerBril2.setForeground(new Color(0,173,173)); invoerKleur2.setForeground(new Color(0,173,173)); invoerLabel.setForeground(new Color(227,227,227)); invoerKleur.setForeground(new Color(227,227,227)); invoerBril.setForeground(new Color(227,227,227)); //vloer g.setColor(new Color(102,77,51)); g.fillRect(0, 375, 1000, 200); //muur g.setColor(new Color(99,136,176)); g.fillRect(0, 0, 1000, 400); //achtergrond kleur balk// g.setColor(new Color(0,102,53)); g.fillRect(980, 0, 386, 766); //beamer g.setColor(new Color(122,122,122)); g.fillRect(355,20,100,30); g.fillRect(397,0,15,40); g.setColor(Color.BLACK); g.fillRect(355, 20, 100, 3); g.fillRect(355, 50, 100, 3); g.fillRect(352, 20, 3, 33); g.fillRect(452, 20, 3, 33); g.fillRect(410, 0, 3, 20); g.fillRect(395, 0, 3, 20); //bord g.setColor(new Color(98,121,74)); g.fillRect(250, 100,300 , 200); g.setColor(Color.BLACK); g.fillRect(250, 300,300 , 10); g.fillRect(250, 100,300 , 10); g.fillRect(250, 100,10 , 200); g.fillRect(550, 100,10 , 210); //boekenkast horizontale balken g.setColor(Color.BLACK); g.fillRect(50, 160, 30, 250); g.fillRect(200, 160, 30, 250); //boekenkast verticale balken g.fillRect(50, 160, 150, 20); g.fillRect(50, 220, 150, 20); g.fillRect(50, 280, 150, 20); g.fillRect(50, 340, 150, 20); //boekenkast boeken rij 1 g.setColor(new Color(204,0,0)); g.fillRect(80, 180, 20, 40); g.setColor(new Color(0,204,102)); g.fillRect(110, 180, 20, 40); g.setColor(new Color(204,102,0)); g.fillRect(140, 180, 20, 40); g.setColor(new Color(204,204,0)); g.fillRect(170, 180, 20, 40); //boekenkast boeken rij 2 g.setColor(new Color(0,204,102)); g.fillRect(80, 240, 20, 40); g.setColor(new Color(204,0,0)); g.fillRect(110, 240, 20, 40); g.setColor(new Color(204,102,0)); g.fillRect(140, 240, 20, 40); g.setColor(new Color(204,204,0)); g.fillRect(170, 240, 20, 40); //boekenkast boeken rij 3 g.setColor(new Color(204,0,0)); g.fillRect(80, 300, 20, 40); g.setColor(new Color(204,102,0)); g.fillRect(110, 300, 20, 40); g.setColor(new Color(204,204,0)); g.fillRect(140, 300, 20, 40); g.setColor(new Color(0,204,102)); g.fillRect(170, 300, 20, 40); //boeklabels g.setColor(new Color(224,224,224)); g.fillRect(85,190,10,20); g.fillRect(115,190,10,20); g.fillRect(145,190,10,20); g.fillRect(175,190,10,20); g.fillRect(85,250,10,20); g.fillRect(115,250,10,20); g.fillRect(145,250,10,20); g.fillRect(175,250,10,20); g.fillRect(85,310,10,20); g.fillRect(115,310,10,20); g.fillRect(145,310,10,20); g.fillRect(175,310,10,20); //hoofd// g.setColor(new Color(255,237,184)); g.fillOval(615,170,150,150); g.setColor(new Color(255,255,255)); g.fillOval(645,220,25,25); g.fillOval(715,220,25,25); g.setColor(new Color(0,0,0)); g.fillOval(655,230,10,10); g.fillOval(725,230,10,10); g.drawArc(675,240,40,40,0,-180); g.drawArc(635,250,115,50,0,-180); //lichaam g.setColor(new Color(153,153,0)); g.fillRect(650, 300, 100, 125); g.setColor(Color.BLACK); g.fillRect(650, 430, 25, 60); g.fillRect(730, 430, 25, 60); g.fillRect(650, 420, 100, 15); g.setColor(Color.BLUE); g.fillOval(632, 470, 45, 25); g.fillOval(725, 470, 45, 25); // bureau g.setColor(new Color(184, 184, 184)); g.fillRect(540, 410, 325, 20); g.fillRect(540, 430, 20, 60); g.fillRect(845, 430, 20, 60); //vingers g.setColor(new Color(255,237,184)); g.fillOval(785, 375, 10, 25); g.fillOval(775, 375, 10, 25); g.fillOval(765, 375, 10, 25); g.fillOval(755, 375, 10, 25); //pc g.setColor(Color.BLACK); g.fillRect(650, 280, 175, 100); g.fillRect(676, 400, 125, 10); g.fillRect(730, 375, 15, 30); // g.setColor(new Color(184, 184, 184)); g.fillRect(660, 320, 20, 5); g.fillRect(660, 340, 20, 5); g.fillRect(795, 320, 20, 5); g.fillRect(795, 340, 20, 5); //apple g.fillOval(725, 320, 20, 20); g.fillOval(732, 310, 5, 10); g.setColor(Color.BLACK); g.fillOval(735, 325, 10, 10); if(strBril.equals("blauw")) { g.setColor(Color.BLUE); } if(strBril.equals("rood")) { g.setColor(Color.RED); } if(strBril.equals("groen")) { g.setColor(Color.GREEN); } if(strBril.equals("paars")) { g.setColor(new Color(204,51,255)); } if(strBril.equals("roos")) { g.setColor(new Color(255,51,204)); } if(strBril.equals("zwart")) { g.setColor(Color.BLACK); } if(strBril.equals("geel")) { g.setColor(Color.YELLOW); } if(strBril.equals("oranje")) { g.setColor(Color.ORANGE); }{ //bril// //linkerglas g.fillRect(600,200,80,5); g.fillRect(600,200,5,50); g.fillRect(600,250,80,5); g.fillRect(680,200,5,55); //tussenbeentje// g.fillRect(680,225,20,10); //rechterglas g.fillRect(700,200,80,5); g.fillRect(700,200,5,50); g.fillRect(700,250,80,5); g.fillRect(780,200,5,55); } int teller1; if(strKleur.equals("blauw")) { g.setColor(Color.BLUE); } if(strKleur.equals("rood")) { g.setColor(Color.RED); } if(strKleur.equals("groen")) { g.setColor(Color.GREEN); } if(strKleur.equals("paars")) { g.setColor(new Color(204,51,255)); } if(strKleur.equals("roos")) { g.setColor(new Color(255,51,204)); } if(strKleur.equals("zwart")) { g.setColor(Color.BLACK); } if(strKleur.equals("geel")) { g.setColor(Color.YELLOW); } if(strKleur.equals("oranje")) { g.setColor(Color.ORANGE); } int Ra = 20; int Rb = 20; int Rc = 40; int Rd = 20; for (teller1=1; teller1 &lt;= intAantal; teller1++) { g.fillRect(Ra,450,10,50); g.fillRect(Rb,500,50,10); g.fillRect(Rc,500,10,30); g.fillRect(Rd,530,50,10); Ra = Ra + 70; Rb = Rb + 70; Rc = Rc + 70; Rd = Rd + 70; } if (booOpAf) { if (!booOpAf) { g.setColor(new Color(153,153,0)); g.fillRect(625, 300, 30, 100); } else { //arm g.setColor(new Color(153,153,0)); g.fillRect(550, 315, 100, 25); //linkerarm vingers g.setColor(new Color(255,237,184)); g.fillOval(530, 310, 30, 30); g.fillOval(515, 310, 25, 10); g.fillOval(515, 320, 25, 10); g.fillOval(515, 330, 25, 10); g.fillOval(545, 300, 10, 25); } } if (booOpAfbeam) { if (!booOpAfbeam) { } else { g.drawImage(image1, 500, 100, null); } } } private void ImageIcon(String string) { // TODO Auto-generated method stub } class InvoervakHandler implements ActionListener { public void actionPerformed(ActionEvent e) { String strAantal = invoerAantal.getText(); intAantal = Integer.parseInt(strAantal); strKleur= invoerK.getText(); repaint(); } } class InvoervakHandler2 implements ActionListener { public void actionPerformed(ActionEvent f){ strBril= invoerB.getText(); repaint(); } } public class aanuit implements ActionListener { public void actionPerformed(ActionEvent arg0) { if (booOpAf == true) { booOpAf = false; } else { booOpAf = true; } repaint(); } } public class beam implements ActionListener { public void actionPerformed(ActionEvent arg0) { if (booOpAfbeam == true) { booOpAfbeam = false; } else { booOpAfbeam = true; } repaint(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload