mouse clicked event in Graphics2D in java - java

hello thanks in advance guys i need to find the X and y coordinates values by Mouse clicking on the draw panel of Graphics2D,and i need to get the string values of the coordinates randomly change the color of the coordinates.
package com.zetcode;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
class DrawPanel extends JPanel {
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(212, 212, 212));
g2d.drawRect(10, 15, 90, 60);
g2d.drawRect(130, 15, 90, 60);
g2d.drawRect(250, 15, 90, 60);
g2d.drawRect(10, 105, 90, 60);
g2d.drawRect(130, 105, 90, 60);
g2d.drawRect(250, 105, 90, 60);
g2d.drawRect(10, 195, 90, 60);
g2d.drawRect(130, 195, 90, 60);
g2d.drawRect(250, 195, 90, 60);
g2d.setColor(new Color(125, 167, 116));
g2d.fillRect(10, 15, 90, 60);
g2d.setColor(new Color(42, 179, 231));
g2d.fillRect(130, 15, 90, 60);
g2d.setColor(new Color(70, 67, 123));
g2d.fillRect(250, 15, 90, 60);
g2d.setColor(new Color(130, 100, 84));
g2d.fillRect(10, 105, 90, 60);
g2d.setColor(new Color(252, 211, 61));
g2d.fillRect(130, 105, 90, 60);
g2d.setColor(new Color(241, 98, 69));
g2d.fillRect(250, 105, 90, 60);
g2d.setColor(new Color(217, 146, 54));
g2d.fillRect(10, 195, 90, 60);
g2d.setColor(new Color(63, 121, 186));
g2d.fillRect(130, 195, 90, 60);
g2d.setColor(new Color(31, 21, 1));
g2d.fillRect(250, 195, 90, 60);
}
public void mouseClicked(MouseEvent me) {
int x1 = me.getX();
int y1 = me.getY();
System.out.println(x1);
JOptionPane.showMessageDialog(this, x1);
JOptionPane.showMessageDialog(this, y1);
//System.out.println(y1);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public class RectanglesExample extends JFrame {
public RectanglesExample() {
initUI();
}
public final void initUI() {
DrawPanel dpnl = new DrawPanel();
add(dpnl);
setSize(360, 300);
setTitle("Rectangles");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
RectanglesExample ex = new RectanglesExample();
ex.setVisible(true);
}
});
}
}

Concerning the printing of the coordinates: Declare an inner class as follows
public class MyMouseListener extends MouseAdapter{
#Override
public void mouseClicked(MouseEvent e) {
int x1 = e.getX();
int y1 = e.getY();
System.out.println(x1);
System.out.println(y1);
repaint();
}
}
and add a constructor which register the MyMouseListener
public DrawPanel(){
this.addMouseListener(new MyMouseListener());
}
This will print you the coordinates every time you click on the panel.

Related

Shapes are not drawn in real time

So, if I try to move the shape, it actually moves, but there is no motion animation. In order for the moved shape to be drawn, the application window must be minimized or maximized.
Here is the PentaminoShape class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class PentominoShape extends JFrame implements MouseListener, MouseMotionListener {
JPanel shapePane;
Container contentPane;
private Polygon currPolygon;
private int x, y;
ArrayList<Polygon> polygons = new ArrayList<Polygon>();
JFrame frame;
public PentominoShape(JFrame frame){
this.frame = frame;
initShape();
}
private void initShape() {
Polygon fig1 = new Polygon(new int[]{10, 50, 50, 10}, new int[]{10, 10, 200, 200}, 4);
Polygon fig2 = new Polygon(new int[]{130, 210, 210, 170, 170, 130, 130, 90, 90, 130}, new int[]{80, 80, 120, 120, 200, 200, 160, 160, 120, 120}, 10);
Polygon fig3 = new Polygon(new int[]{290, 330, 330, 250, 250, 290}, new int[]{50, 50, 200, 200, 160, 160}, 6);
Polygon fig4 = new Polygon(new int[]{10, 90, 90, 50, 50, 10}, new int[]{280, 280, 400, 400, 360, 360}, 6);
Polygon fig5 = new Polygon(new int[]{170, 210, 210, 170, 170, 130, 130, 170}, new int[]{240, 240, 360, 360, 400, 400, 320, 320}, 8);
Polygon fig6 = new Polygon(new int[]{250, 370, 370, 330, 330, 290, 290, 250}, new int[]{280, 280, 320, 320, 400, 400, 320, 320}, 8);
Polygon fig7 = new Polygon(new int[]{10, 50, 50, 90, 90, 130, 130, 10}, new int[]{480, 480, 520, 520, 480, 480, 560, 560}, 8);
Polygon fig8 = new Polygon(new int[]{170, 250, 250, 290, 290, 170}, new int[]{520, 520, 440, 440, 560, 560}, 6);
Polygon fig9 = new Polygon(new int[]{330, 370, 370, 410, 410, 450, 450, 410, 410, 330}, new int[]{520, 520, 480, 480, 440, 440, 520, 520, 560, 560}, 10);
Polygon fig10 = new Polygon(new int[]{10, 50, 50, 90, 90, 130, 130, 90, 90, 50, 50, 10}, new int[]{680, 680, 640, 640, 680, 680, 720, 720, 760, 760, 720, 720}, 12);
Polygon fig11 = new Polygon(new int[]{170, 210, 210, 250, 250, 210, 210, 170}, new int[]{640, 640, 600, 600, 760, 760, 680, 680}, 8);
Polygon fig12 = new Polygon(new int[]{330, 410, 410, 370, 370, 290, 290, 330}, new int[]{640, 640, 680, 680, 760, 760, 720, 720}, 8);
polygons.add(fig1);
polygons.add(fig2);
polygons.add(fig3);
polygons.add(fig4);
polygons.add(fig5);
polygons.add(fig6);
polygons.add(fig7);
polygons.add(fig8);
polygons.add(fig9);
polygons.add(fig10);
polygons.add(fig11);
polygons.add(fig12);
Color[] c = new Color[12];
c[0] = new Color(25, 165, 25);
c[1] = new Color(255, 165, 25);
c[2] = new Color(255, 50, 50);
c[3] = new Color(150, 45, 90);
c[4] = new Color(25, 165, 150);
c[5] = new Color(25, 165, 255);
c[6] = new Color(255, 40, 190);
c[7] = new Color(180, 90, 60);
c[8] = new Color(90, 80, 70);
c[9] = new Color(70, 80, 90);
c[10] = new Color(150, 30, 20);
c[11] = new Color(80, 80, 80);
shapePane = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(c[0]); g2.fill(fig1);
g2.setColor(c[1]); g2.fill(fig2);
g2.setColor(c[2]); g2.fill(fig3);
g2.setColor(c[3]); g2.fill(fig4);
g2.setColor(c[4]); g2.fill(fig5);
g2.setColor(c[5]); g2.fill(fig6);
g2.setColor(c[6]); g2.fill(fig7);
g2.setColor(c[7]); g2.fill(fig8);
g2.setColor(c[8]); g2.fill(fig9);
g2.setColor(c[9]); g2.fill(fig10);
g2.setColor(c[10]); g2.fill(fig11);
g2.setColor(c[11]); g2.fill(fig12);
}
};
/*contentPane = this.getContentPane();
contentPane.add(shapePane);
this.pack();*/
frame.add(shapePane);
shapePane.addMouseListener(this);
shapePane.addMouseMotionListener(this);
/*shapePane.addMouseListener(this);
shapePane.addMouseMotionListener(this);*/
}
public void mousePressed(MouseEvent e) {
for(Polygon polygon: polygons) {
if (polygon.contains(e.getPoint())) {
System.out.println("Pressed");
currPolygon = polygon;
x = e.getX();
y = e.getY();
}
}
}
public void mouseDragged(MouseEvent e) {
try {
if (currPolygon.contains(x, y)) {
System.out.println("Dragged");
int dx = e.getX() - x;
int dy = e.getY() - y;
currPolygon.translate(dx, dy);
x += dx;
y += dy;
repaint();
}
}catch (NullPointerException ex){
}
}
public void mouseReleased(MouseEvent e){
currPolygon = null;
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
}
And the main Pentamino class:
import javax.swing.*;
public class Pentomino extends JFrame {
JFrame frame;
PentominoShape shape;
PentominoPanel panel;
public Pentomino(){
initUI();
}
private void initUI(){
frame = new JFrame("Пентамино");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(1500, 900);
setResizable(false);
shape = new PentominoShape(frame);
panel = new PentominoPanel(frame);
/*frame.add(shape);
frame.add(panel);*/
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
Pentomino game = new Pentomino();
}
}
I'm new to Java and I don't understand how to solve this problem. Tried searching the internet for a similar problem but couldn't find anything.
Your bug is here:
public void mouseDragged(MouseEvent e) {
try {
if (currPolygon.contains(x, y)) {
System.out.println("Dragged");
int dx = e.getX() - x;
int dy = e.getY() - y;
currPolygon.translate(dx, dy);
x += dx;
y += dy;
repaint(); // ***** here ****
}
}catch (NullPointerException ex){
}
}
You're calling repaint() on the enclosing class, a JFrame, one that is never displayed, and this will have not have the effect desired.
In fact, ask yourself, why this...
public class PentominoShape extends JFrame // ...
Why is PentominoShape extending JFrame at all, when it isn't behaving as a JFrame, when it shouldn't be behaving as a JFrame?
Instead, call repaint on the JPanel that holds the shapes, the shapePane JPanel, and yes, get rid of catching the NullPointerException. That should never be in your code:
public void mouseDragged(MouseEvent e) {
if (currPolygon == null) {
return;
}
if (currPolygon.contains(x, y)) {
System.out.println("Dragged");
int dx = e.getX() - x;
int dy = e.getY() - y;
currPolygon.translate(dx, dy);
x += dx;
y += dy;
shapePane.repaint(); // now we're repainting the correct JPanel!
}
}
Side note: I'd clean things up a bit including
Not having any class extend JFrame if possible
Create my own polygon type of class:
public class PentominoShape2 {
private Polygon polygon;
private Color color;
public PentominoShape2(Polygon polygon, Color color) {
this.polygon = polygon;
this.color = color;
}
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(color);
g2.fill(polygon);
}
public Polygon getPolygon() {
return polygon;
}
public Color getColor() {
return color;
}
public boolean contains(Point p) {
return polygon.contains(p);
}
}
and then in the drawing JPanel
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (PentominoShape2 poly : polys) {
poly.draw(g);
}
}
same for the mouse listener

EasterEgg in Java

package easteregg;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
g.drawOval (100, 20, 110, 160);
}
}
public class EasterEgg {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}
my problem is i dont know how to put lines inside the oval
i want to make a easterEgg please help me
i have modified the paint method to draw one pattern please check the below code
public void paint(Graphics g) {
g.drawOval (100, 20, 110, 160);
g.drawLine(120, 40, 140, 60);
g.drawLine(140, 60, 150, 40);
g.drawLine(150, 40, 170, 60);
g.drawLine(170, 60, 190,40);
}
and the output is
this is just a hint to draw lines but better to create one method which will draw that pattern form one end to other end of the oval

Panel wont show in the window

I am trying to start the program with jcheckbox hat selected and rectangle visible then the Rectangle disappears when the checkbox is unselected and repainted as checkbox is selected again. When I run the program and check the box another check box appears or left of the frame.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class Head extends JPanel {
JCheckBox hat;
public Head() {
hat = new JCheckBox("Hat");
hat.setSelected(true);
hat.addItemListener(new CheckSelection());
add(hat);
}
class CheckSelection implements ItemListener {
public void itemStateChanged(ItemEvent ie) {
repaint();
}
}
public void paintComponent(Graphics g) {
setForeground(Color.RED);
g.drawOval(110, 100, 100, 100);
g.drawOval(130, 120, 20, 15);
g.drawOval(170, 120, 20, 15);
g.drawLine(160, 130, 160, 160);
g.drawOval(140, 170, 40, 15);
if (hat.isSelected()) {
g.drawRect(100, 90, 120, 10);
}
}
public static void main(String[] args) {
Head head = new Head();
JFrame f = new JFrame();
f.add(head);
f.setSize(400, 400);
//f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You've broken the paint chain by not calling the paintComponent's super method
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setForeground(Color.RED);
g.drawOval(110, 100, 100, 100);
g.drawOval(130, 120, 20, 15);
g.drawOval(170, 120, 20, 15);
g.drawLine(160, 130, 160, 160);
g.drawOval(140, 170, 40, 15);
if (hat.isSelected()) {
g.drawRect(100, 90, 120, 10);
} else {
setForeground(Color.RED);
g.drawOval(110, 100, 100, 100);
g.drawOval(130, 120, 20, 15);
g.drawOval(170, 120, 20, 15);
g.drawLine(160, 130, 160, 160);
g.drawOval(140, 170, 40, 15);
}
}
The Graphics context is a shared resource between components, one of the jobs of paintComponent is to prepare the Graphics for painting, typically by filling it with the background color of the component. So failing to call super.paintComponent means that what ever was previously painted to the Graphics context will still be there
See Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing

I am trying to make a triangle out of a sloop can in Java

I am trying to make a triangle out of a sloop can a made in the graphics panel but I cant seem to get the code to work right. this is what I have so far.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class PyramidSoupCans {
/**
* #param args
*/
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(800, 800);
panel.setBackground(new Color(219, 204, 186));
Graphics g = panel.getGraphics();
for(int i=1;i<=10;i++){
for(int j=1;j<=10-i; j++){
System.out.print(" ");
}
for (int k=1;k<=2 * i-1;k++){
draw_can( g, i, k);
}
System.out.println();
}}
public static void draw_can(Graphics g, int x, int y){
int sizeX = 225;
int sizeY = 340;
int cornerX = 37;
int cornerY = 80;
g.setColor(new Color(138, 138, 138));
g.fillRoundRect(cornerX, 246, sizeX, 180, 150, 45);
g.setColor(new Color(243, 243, 243));
g.fillRoundRect(cornerX, cornerY, sizeX, sizeY, 150, 45);
g.setColor(new Color(162,22,5));
g.fillRoundRect(cornerX, 70, sizeX, 190, 150, 45);
g.setColor(new Color(138, 138, 138));
g.fillOval(cornerX, 65, sizeX, 45);
g.setColor(Color.white);
g.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,45));
g.drawString("Campbell's",45,150);
g.setFont(new Font("SanSerif",Font.BOLD,20));
g.drawString("CONDENSED",85,200);
g.setColor(new Color(162,22,5));
g.setFont(new Font("SanSerif",Font.BOLD,40));
g.drawString("TOMATO",60,360);
g.setFont(new Font("Serif",Font.BOLD,40));
g.setColor(new Color(157, 131, 82));
g.fillOval(113, 223, 70, 70);
g.drawString("SOUP",95,410);
}}
I am trying to make a triangle out of a sloop can a made in the graphics panel but I cant seem to get the code to work right. this is what I have so far.
That's all. Thanks
I wasn't able to make a triangle, but I did get the soup can to draw.
I made lots of changes to your code. I did keep your class name, PyramidSoupCans.
The important changes include.
I decided to use Swing components, since you named a DrawingPanel that you didn't include as part of your code.
You always start a Swing application with a call to the SwingUtilities invokeLater method. This ensures that the Swing components are created and updated on the Event Dispatch thread.
I created a drawing panel from a JPanel, and put the JPanel in a JFrame with some basic decorations.
I put your drawing code in the place where Swing provides a Graphics object to draw on. You should always override the JPanel paintComponent method when you want to draw on a Swing GUI.
Here's the code:
package com.ggl.testing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PyramidSoupCans implements Runnable {
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new PyramidSoupCans());
}
#Override
public void run() {
frame = new JFrame("Pyramid Soup Cans");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawingPanel panel = new DrawingPanel(300, 500);
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = -4147433638611078320L;
public DrawingPanel(int width, int height) {
this.setPreferredSize(new Dimension(width, height));
this.setBackground(new Color(219, 204, 186));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int sizeX = 225;
int sizeY = 340;
int cornerX = 37;
int cornerY = 80;
g.setColor(new Color(138, 138, 138));
g.fillRoundRect(cornerX, 246, sizeX, 180, 150, 45);
g.setColor(new Color(243, 243, 243));
g.fillRoundRect(cornerX, cornerY, sizeX, sizeY, 150, 45);
g.setColor(new Color(162, 22, 5));
g.fillRoundRect(cornerX, 70, sizeX, 190, 150, 45);
g.setColor(new Color(138, 138, 138));
g.fillOval(cornerX, 65, sizeX, 45);
g.setColor(Color.white);
g.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 45));
g.drawString("Campbell's", 45, 150);
g.setFont(new Font("SanSerif", Font.BOLD, 20));
g.drawString("CONDENSED", 85, 200);
g.setColor(new Color(162, 22, 5));
g.setFont(new Font("SanSerif", Font.BOLD, 40));
g.drawString("TOMATO", 60, 360);
g.setFont(new Font("Serif", Font.BOLD, 40));
g.setColor(new Color(157, 131, 82));
g.fillOval(113, 223, 70, 70);
g.drawString("SOUP", 95, 410);
}
}
}

Java - how to add a png file to JPanel

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 java.png, 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!
Note: the image should be inserted in the booOpAfbeam 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.
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 <= 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();
}
}
}
I cant verify your complete code. so better ensure that u have imported the image to the project(else u wont get the image if the image is not in the location specified). and also check if the image is in the src, u need to specify "/" and the image name. to locate the image.
I suggest the best way is to create a label,the right click and take the property of the image and select the image which u wants to display. rest of the codes netbeans will take care of.
Sorry if i made any mistake.
Try this:
Add another panel and then upload image on that panel.
lblImage = new javax.swing.JLabel();
lblImage.setIcon(new javax.swing.ImageIcon("E:..path...png));

Categories

Resources