I have a lot of QuadCurve2D methods in a drawing, but when I fill them they don't actually fill the whole image.
Code for fill and curves:
g2.setStroke(new BasicStroke(5));
QuadCurve2D earLeft1 = new QuadCurve2D.Double(145, 155, 137.5, 49, 150, 49);
g2.draw(earLeft1);
QuadCurve2D earLeft2 = new QuadCurve2D.Double(150, 49, 156.25, 49, 200, 100);
g2.draw(earLeft2);
QuadCurve2D betweenEars = new QuadCurve2D.Double(200, 100, 237.5, 88, 262.5, 87.5);
g2.draw(betweenEars);
QuadCurve2D earRight1 = new QuadCurve2D.Double(262.5, 87.5, 287.5, 25, 300, 25);
g2.draw(earRight1);
QuadCurve2D earRight2 = new QuadCurve2D.Double(300, 25, 312.5, 25, 337.5, 137.5);
g2.draw(earRight2);
CubicCurve2D headPhoneLeft = new CubicCurve2D.Double(145, 155, 75, 175, 100, 250, 150, 250);
g2.draw(headPhoneLeft);
CubicCurve2D headPhoneRight = new CubicCurve2D.Double(337.5, 137.5, 387.5, 137.5, 393.75, 188, 362.5, 225);
g2.draw(headPhoneRight);
QuadCurve2D headbandTop1 = new QuadCurve2D.Double(109, 177, 150, 75, 225, 50);
g2.draw(headbandTop1);
QuadCurve2D headbandTop2 = new QuadCurve2D.Double(225, 50, 300, 50, 372, 150);
g2.draw(headbandTop2);
QuadCurve2D headbandBottom1 = new QuadCurve2D.Double(135, 155, 150, 112.5, 212.5, 78);
g2.draw(headbandBottom1);
QuadCurve2D headbandBottom2 = new QuadCurve2D.Double(212.5, 78, 306.25, 78, 351, 137.5);
g2.draw(headbandBottom2);
QuadCurve2D faceBottomLeft = new QuadCurve2D.Double(150, 250, 162.5, 275, 200, 300);
g2.draw(faceBottomLeft);
QuadCurve2D faceBottomRight = new QuadCurve2D.Double(362.5, 225, 363.5, 237.5, 350, 262.5);
g2.draw(faceBottomRight);
CubicCurve2D leftArm = new CubicCurve2D.Double(200, 300, 87.5, 300, 87.5, 375, 188.5, 362.5);
g2.draw(leftArm);
CubicCurve2D rightArm = new CubicCurve2D.Double(350, 262.5, 425, 237.5, 450, 300, 375, 325);
g2.draw(rightArm);
QuadCurve2D leftLegOuter = new QuadCurve2D.Double(188.5, 362.5, 154, 425, 200, 512.5);
g2.draw(leftLegOuter);
QuadCurve2D rightLegOuter = new QuadCurve2D.Double(375, 325, 388.5, 356.25, 387.5, 412.5);
g2.draw(rightLegOuter);
QuadCurve2D leftFootTop = new QuadCurve2D.Double(200, 512.5, 125, 500, 130, 562.5);
g2.draw(leftFootTop);
QuadCurve2D leftFootBottom = new QuadCurve2D.Double(130, 562.5, 175, 575, 262.5, 562.5);
g2.draw(leftFootBottom);
QuadCurve2D leftLegInner = new QuadCurve2D.Double(262.5, 562.5, 237.5, 400, 268.75, 363);
g2.draw(leftLegInner);
QuadCurve2D rightLegInner = new QuadCurve2D.Double(268.75, 363, 318.75, 362.5, 337.5, 475);
g2.draw(rightLegInner);
QuadCurve2D rightFootBottom = new QuadCurve2D.Double(337.5, 475, 400, 480, 455, 470);
g2.draw(rightFootBottom);
QuadCurve2D rightFootTop = new QuadCurve2D.Double(455, 470, 450, 400, 387.5, 412.5);
g2.draw(rightFootTop);
CubicCurve2D tailInner = new CubicCurve2D.Double(268.75, 363, 287.5, 450,125, 387.5, 62.5, 400);
g2.draw(tailInner);
//QuadCurve2D tailInner1 = new QuadCurve2D.Double(268.75, 363, 275, 387.5, 200, 400);
//g2.draw(tailInner1);
//QuadCurve2D tailInner2 = new QuadCurve2D.Double(200, 400, 125, 387.5, 62.5, 400);
//g2.draw(tailInner2);
QuadCurve2D tailOuter1 = new QuadCurve2D.Double(62.5, 400, 25, 425, 200, 437.5);
g2.draw(tailOuter1);
QuadCurve2D tailOuter2 = new QuadCurve2D.Double(200, 437.5, 287.5, 425, 300, 375);
g2.draw(tailOuter2);
int[] x = {175, 200, 225, 225, 287, 300, 309, 337, 325, 309, 302, 292, 240, 227, 226, 215};
int[] y = {225, 210, 237, 200, 187, 212, 187, 187, 262, 262, 230, 268, 275, 250, 277, 281};
GeneralPath mouthAndTeeth = new GeneralPath();
mouthAndTeeth.moveTo(x[0], y[0]);
for (int index = 1; index < x.length; index++) {
mouthAndTeeth.lineTo(x[index], y[index]);
};
mouthAndTeeth.closePath();
g2.draw(mouthAndTeeth);
/*
g2.fill(earLeft1);
g2.fill(earLeft2);
g2.fill(earRight1);
g2.fill(earRight2);
g2.fill(betweenEars);
g2.fill(headbandBottom1);
g2.fill(headbandBottom2);
g2.fill(headbandTop1);
g2.fill(headbandTop2);
g2.fill(headPhoneLeft);
g2.fill(headPhoneRight);
g2.fill(faceBottomLeft);
g2.fill(faceBottomRight);
g2.fill(leftArm);
g2.fill(rightArm);
g2.fill(leftFootBottom);
g2.fill(leftFootTop);
g2.fill(rightFootBottom);
g2.fill(rightFootTop);
g2.fill(leftLegInner);
g2.fill(leftLegOuter);
g2.fill(rightLegInner);
g2.fill(rightLegOuter);
//g2.fill(tailInner1);
//g2.fill(tailInner2);
g2.fill(tailOuter1);
g2.fill(tailOuter2);
*/
This has been my only solution but it's not doing it. Suggestions?
"A" solution might be to paint the "shape" to a BufferedImage and simply scale the image, but this will scale the stroke/line size as well.
A better solution might be to encapsulate the drawing into a Shape and use Shape#createTransformedShape instead.
public class Cat extends Path2D.Double {
public Cat() {
//Left Ear
moveTo(145, 155);
curveTo(145, 155, 137.5, 49, 150, 49);
moveTo(150, 49);
curveTo(150, 49, 156.25, 49, 200, 100);
// Between Ears
moveTo(200, 100);
curveTo(200, 100, 237.5, 88, 262.5, 87.5);
// Right ear
moveTo(262.5, 87.5);
curveTo(262.5, 87.5, 287.5, 25, 300, 25);
moveTo(300, 25);
curveTo(300, 25, 312.5, 25, 337.5, 137.5);
// Head phone left
moveTo(300, 25);
append(new CubicCurve2D.Double(145, 155, 75, 175, 100, 250, 150, 250), false);
moveTo(337.5, 137.5);
append(new CubicCurve2D.Double(337.5, 137.5, 387.5, 137.5, 393.75, 188, 362.5, 225), false);
moveTo(109, 177);
curveTo(109, 177, 150, 75, 225, 50);
moveTo(225, 50);
curveTo(225, 50, 300, 50, 372, 150);
moveTo(135, 155);
curveTo(135, 155, 150, 112.5, 212.5, 78);
moveTo(212.5, 78);
curveTo(212.5, 78, 306.25, 78, 351, 137.5);
moveTo(150, 250);
curveTo(150, 250, 162.5, 275, 200, 300);
moveTo(362.5, 225);
curveTo(362.5, 225, 363.5, 237.5, 350, 262.5);
moveTo(200, 300);
append(new CubicCurve2D.Double(200, 300, 87.5, 300, 87.5, 375, 188.5, 362.5), false);
moveTo(350, 262.5);
append(new CubicCurve2D.Double(350, 262.5, 425, 237.5, 450, 300, 375, 325), false);
moveTo(188.5, 362.5);
curveTo(188.5, 362.5, 154, 425, 200, 512.5);
moveTo(375, 325);
curveTo(375, 325, 388.5, 356.25, 387.5, 412.5);
moveTo(200, 512.5);
curveTo(200, 512.5, 125, 500, 130, 562.5);
moveTo(130, 562.5);
curveTo(130, 562.5, 175, 575, 262.5, 562.5);
moveTo(262.5, 562.5);
curveTo(262.5, 562.5, 237.5, 400, 268.75, 363);
moveTo(268.75, 363);
curveTo(268.75, 363, 318.75, 362.5, 337.5, 475);
moveTo(337.5, 475);
curveTo(337.5, 475, 400, 480, 455, 470);
moveTo(455, 470);
curveTo(455, 470, 450, 400, 387.5, 412.5);
moveTo(268.75, 363);
append(new CubicCurve2D.Double(268.75, 363, 287.5, 450, 125, 387.5, 62.5, 400), false);
moveTo(62.5, 400);
curveTo(62.5, 400, 25, 425, 200, 437.5);
moveTo(200, 437.5);
curveTo(200, 437.5, 287.5, 425, 300, 375);
int[] x = {175, 200, 225, 225, 287, 300, 309, 337, 325, 309, 302, 292, 240, 227, 226, 215};
int[] y = {225, 210, 237, 200, 187, 212, 187, 187, 262, 262, 230, 268, 275, 250, 277, 281};
GeneralPath mouthAndTeeth = new GeneralPath();
mouthAndTeeth.moveTo(x[0], y[0]);
for (int index = 1; index < x.length; index++) {
mouthAndTeeth.lineTo(x[index], y[index]);
}
mouthAndTeeth.closePath();
append(mouthAndTeeth, false);
}
}
And then scale the shape based on the available space of the container...
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Cat cat;
public TestPane() {
cat = new Cat();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(new BasicStroke(5));
int catWidth = cat.getBounds().x + cat.getBounds().width;
int catHeight = cat.getBounds().y + cat.getBounds().height;
int width = getWidth() - 1;
int height = getHeight() - 1;
double scaleWidth = width / (double)catWidth;
double scaleHeight = height / (double)catHeight;
double scale = Math.min(scaleHeight, scaleWidth);
AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
Shape shape = cat.createTransformedShape(at);
g2d.draw(shape);
g2d.dispose();
}
}
}
Which you use will come down to needs, for example, using a Shape like this, won't allow you to color individual sections differently
Related
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
I'm trying to make/add functions to a hang-man game and I stumbled upon the following problems when trying to display the letters that are correctly guessed.
The part of the code that I am implying on is meant to work as follows:
When a letter is correctly guessed index is supposed to get the index the following letter was placed in, in the char array: randomWord.
What I want to happen after that is for the array: displayWord to be set on the label: rättord and then for every letter that is correctly guessed the index that contains that letter gets visible.
How do I add the array: displayWord to the JLabel: rättord?
I want to do this with the purpose of only revealing one letter a time in the array.
The goal is to make every letter that is correctly guessed appear in the JLabel rättord, any other methods of displaying correctly guessed words are welcome.
(the current code )
The code:
package hangmann;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class HangMan extends JPanel implements ActionListener, MouseListener {
private int error;
JButton button;
JTextField field;
JFrame frame;
JLabel rättord;
HangMan(JButton button, JTextField field, JFrame frame, JLabel rättord) {
this.button = button;
this.field = field;
this.frame = frame;
this.rättord = rättord;
this.addMouseListener(this);
}
static String [] ord = {"london", "stockholm", "berlin", "rome", "" };
static Random r = new Random();
static int randomNumber=r.nextInt(ord.length);
static String d = ord[randomNumber];
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String guessedWord = field.getText();
if (source.equals(button)) {
if (!d.contains(guessedWord)) {
error++;
frame.repaint();
rättord.setText(d);
}
if (d.contains(guessedWord)) {
char [] randomWord = d.toCharArray();
char CharGuessedWord = guessedWord.charAt(0);
int index = 0;
for(int i = 0; i < randomWord.length; i++ ) {
if(randomWord[i] == CharGuessedWord) {
index = 1+ i;}
else {
i = i+ 1 ;
}
}
char [] displayWord = d.toCharArray();
rättord.setText(Integer.toString(index));
}
field.setText("");
}
}
public void paintComponent(Graphics g ) {
super.paintComponent(g);
if (error == 1)
g.drawLine(10, 270, 500, 270);
if (error == 2) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
}
if (error == 3) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
}
if (error == 4) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
}
if (error == 5) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
}
if (error == 6) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
}
if (error == 7) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
}
if (error == 8) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
}
if (error == 9) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
g.drawLine(350, 200, 310, 240);
}
if (error == 9) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
g.drawLine(350, 200, 310, 240);
}
if (error == 10) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
g.drawLine(350, 200, 310, 240);
g.drawLine(350, 170, 400, 150);
}
if (error == 11) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
g.drawLine(350, 200, 310, 240);
g.drawLine(350, 170, 400, 150);
g.drawLine(350, 170, 300, 150);
}
if(error > 11) {
g.setFont(new Font("Calibri", Font.BOLD, 26));
g.drawString("GAME OVER", 225, 150);
}
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("HangMan");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel rubrikOrd = new JLabel("rätt gissade ord");
rubrikOrd.setOpaque(false);
rubrikOrd.setBackground(Color.GREEN);
rubrikOrd.setBounds(10, 10, 100, 20);
JLabel rättord = new JLabel();
rättord.setOpaque(true);
rättord.setBackground(Color.GREEN);
rättord.setBounds(10, 35, 100, 20);
JTextField field = new JTextField();
field.setVisible(true);
JButton b = new JButton("ok");
b.setBounds(370, 300, 100, 30);
frame.add(b);
field.setSize(300, 30);
field.setLocation(60, 300);
field.setVisible(true);
frame.add(field);
frame.add(rättord);
frame.add(rubrikOrd);
frame.setBackground(Color.white);
frame.setSize(600, 400);
HangMan object = new HangMan(b, field, frame, rättord);
b.addActionListener(object);
field.addActionListener(object);
frame.add(object);
frame.setVisible(true);
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getX() + " " + e.getY());
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
The part I'm referring to:
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String guessedWord = field.getText();
if (source.equals(button)) {
if (!d.contains(guessedWord)) {
error++;
frame.repaint();
rättord.setText(d);
}
if (d.contains(guessedWord)) {
char [] randomWord = d.toCharArray();
char CharGuessedWord = guessedWord.charAt(0);
int index = 0;
for(int i = 0; i < randomWord.length; i++ ) {
if(randomWord[i] == CharGuessedWord) {
index = 1+ i;}
else {
i = i+ 1 ;
}
}
char [] displayWord = d.toCharArray();
rättord.setText(Integer.toString(index));
}
field.setText("");
}
}
I cant comment, but isnt it possible for you to use a char array with same size as the randomWord, but all initialized to maybe an * or _, and on correct guess, replace the index with the char, then you can use a StringBuilder as mentioned by #camickr or you can use some custom toString() or other custom method to return the whole string representation of array elements and setText() for your label
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
i'm currently adding functionality and completing a Hang-Man game my programing teacher made.
The following error message: Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: index 0, length 0 with the index changing for everyindex in a String builder. is appearing everytime i input a letter that is correctly guessed in the game.
I have tried for a while to fix it but i have yet to be able to.
package hangManSo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class HangManSO extends JPanel implements ActionListener, MouseListener {
private int error;
JButton button;
JTextField field;
JFrame frame;
JLabel rättord;
HangManSO(JButton button, JTextField field, JFrame frame, JLabel rättord) {
this.button = button;
this.field = field;
this.frame = frame;
this.rättord = rättord;
this.addMouseListener(this);
}
static String [] ord = {"rome"};
static Random r = new Random();
static int randomNumber=r.nextInt(ord.length);
static String d = ord[randomNumber];
StringBuilder builder = new StringBuilder(d.length());
StringBuilder builderDisplay = new StringBuilder();
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String guessedletter = field.getText();
if (source.equals(button)) {
if (!d.contains(guessedletter)) {
error++;
frame.repaint();
}
if (d.contains(guessedletter)) {
char [] randomWord = d.toCharArray();
char CharGuessedLetter = guessedletter.charAt(0);
//gets the index of the guessed letter in the randomword
builder.append(randomWord);
int index = builder.indexOf(String.valueOf(CharGuessedLetter));
//is supposed to set the correctly guessed letter in the correct index
builderDisplay.setCharAt(index, CharGuessedLetter);
rättord.setText(builderDisplay.toString());
}
field.setText("");
}
}
public void paintComponent(Graphics g ) {
super.paintComponent(g);
if (error == 1)
g.drawLine(10, 270, 500, 270);
if (error == 2) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
}
if (error == 3) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
}
if (error == 4) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
}
if (error == 5) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
}
if (error == 6) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
}
if (error == 7) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
}
if (error == 8) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
}
if (error == 9) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
g.drawLine(350, 200, 310, 240);
}
if (error == 9) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
g.drawLine(350, 200, 310, 240);
}
if (error == 10) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
g.drawLine(350, 200, 310, 240);
g.drawLine(350, 170, 400, 150);
}
if (error == 11) {
g.drawLine(10, 270, 500, 270);
g.drawLine(200, 30, 200, 270);
g.drawLine(200, 30, 350, 30);
g.drawLine(200, 30, 350, 30);
g.drawLine(250, 30, 200, 75);
g.drawLine(350, 30, 350,100);
g.drawOval(330, 100, 40, 40);
g.drawLine(350, 140, 350,200);
g.drawLine(350, 200,390, 240);
g.drawLine(350, 200, 310, 240);
g.drawLine(350, 170, 400, 150);
g.drawLine(350, 170, 300, 150);
}
if(error > 11) {
g.setFont(new Font("Calibri", Font.BOLD, 26));
g.drawString("GAME OVER", 225, 150);
}
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("HangMan");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel rubrikOrd = new JLabel("rätt gissade ord");
rubrikOrd.setOpaque(false);
rubrikOrd.setBackground(Color.GREEN);
rubrikOrd.setBounds(10, 10, 100, 20);
JLabel rättord = new JLabel();
rättord.setOpaque(true);
rättord.setBackground(Color.GREEN);
rättord.setBounds(10, 35, 100, 20);
JTextField field = new JTextField();
field.setVisible(true);
JButton b = new JButton("ok");
b.setBounds(370, 300, 100, 30);
frame.add(b);
field.setSize(300, 30);
field.setLocation(60, 300);
field.setVisible(true);
frame.add(field);
frame.add(rättord);
frame.add(rubrikOrd);
frame.setBackground(Color.white);
frame.setSize(600, 400);
HangManSO object = new HangManSO(b, field, frame, rättord);
b.addActionListener(object);
field.addActionListener(object);
frame.add(object);
frame.setVisible(true);
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getX() + " " + e.getY());
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
The code is needlessly complex and that is making it hard for you to see the issue.
Your actual issue starts on this line where you assign the entire correct word d to randomWord:
char [] randomWord = d.toCharArray();
//randomWord now equals `{r,o,m,e}`
You then use the entire correct word and append it to your builder string:
//builder equals a blank string of 4 characters " "
builder.append(randomWord);
//builder now equals a string with 4 blank characters followed by some " rome"
System.out.print(builder.toString());
So now when you try to get the index of your letter for example if you guessed 'm' it will return a large index:
//the builder string now " rome"
int index = builder.indexOf(String.valueOf(CharGuessedLetter));
//So `m` will be found at the 6th index of " rome"
System.out.print("index = " + index);
So when you use the next line with an index of 6 it will be out of bounds because the builderDisplay String is only 4 characters long for the word rome:
builderDisplay.setCharAt(index, CharGuessedLetter);
So what can you do to fix this? I won't give you an exact answer, but You should start by thinking about the above code, and where it goes wrong (append), and how you can return the correct character index (using the d string)
Final hint, the following line would get a correct character index of a character, note how we use the d string, not the builder string:
int index = d.indexOf(String.valueOf(CharGuessedLetter));
Edit:
Following on from comments. You also need to fill the string builder with blanks to avoid this issue:
//Create the hangman object
HangManSO object = new HangManSO(b, field, frame, rättord);
//Now populate the string builder with empty characters "_" or you could use a space " "
for (int i = 0; i < object.d.length(); i++) {
object.builderDisplay.append('_');
}
Note also that you should check for duplicate characters in your word:
//check against every letter of the word to make sure that it gets duplicate characters
for (int i = 0; i < d.length(); i++) {
if (d.charAt(i) == charGuessedLetter){
builderDisplay.setCharAt(i, charGuessedLetter);
System.out.println("Match found");
}
}
rättord.setText(builderDisplay.toString());
So, I am going to have more of these panels but I don't know how to make these panels switch when the user will press a button like start or next.
//IngestionOfDigestion.java teaches the user about the digestive system through interactive components
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class IngestionOfDigestion extends JFrame {
public static void main(String[] args) {
IngestionOfDigestion i = new IngestionOfDigestion();
}
public IngestionOfDigestion() {//frame header
//make the frame
super("Ingestion Of Digestion");
setSize(1900, 1100);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(10, 50);
setResizable(true);
layout t = new layout();
add(t);
setContentPane(t);
setVisible(true);
}
}
class layout extends JPanel {
public Mouth m;
public CardLayout cardlay = new CardLayout();
Title title = new Title();
public layout() {
m = new Mouth();
cardlay.show();
setLayout(cardlay);
add(m, "Mouth");
add(title, "Title");
}
}
class Title extends JPanel implements ActionListener {//make title panel
private JButton c;//jbutton to start the game
private boolean startpressed;//button pressed variable
//JPanel card2;
//JPanel card3;
public Title() {//title method header
//cardstack = new JPanel(cardlay);
c = new JButton("Press to start playing");//make jbutton saying start the game
c.addActionListener(this);
add(c);
startpressed = false;
}
public void paintComponent(Graphics g) { //paint component
super.paintComponent(g);//super.paintComponent
Font title = new Font("Serif", Font.BOLD, 90);//make font
g.setFont(title);
g.drawString("Ingestion Of Digestion", 160, 100);//draw the title
if (startpressed == true) {//if buttonpressed is true
repaint();
}
}
public void actionPerformed(ActionEvent e) {//ActionPerformed
if (e.getSource() == c) {
startpressed = true;//change boolean
cardlay.next();
repaint();
}
}
}
//if button is pressed
//end title panel
class Mouth extends JPanel implements ActionListener, ChangeListener {
private JButton chew;//make chew button
private JButton next;//next button
private JSlider saliva;//make saliva slider
private boolean nextpressed;//nextbuttonpressed variable
private boolean chewpressed;//buttonpressed variable
Mouth() {//mouth method
chew = new JButton("Press to chew the food");
chew.addActionListener(this);
add(chew);
chewpressed = false;
JSlider saliva = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
add(saliva);
next = new JButton("Press to go to the next step of digestion");
next.addActionListener(this);
add(next);
nextpressed = false;
}
public void paintComponent(Graphics g) {//paint component
super.paintComponent(g);//super.paintComponent
//draw the head
Color skin = new Color(255, 204, 133);
g.setColor(skin);
g.fillArc(810, 1000, 550, 550, 0, 180);
g.fillRect(1085, 600, 150, 550);
g.fillRect(700, 250, 535, 380);
int[] w = {700, 700, 660};
int[] t = {340, 420, 420};
g.fillPolygon(w, t, 3);
Color hair = new Color(100, 60, 0);
g.setColor(hair);
g.fillArc(700, 175, 535, 150, 0, 180);
g.setColor(Color.WHITE);
g.fillOval(730, 275, 45, 45);
g.setColor(Color.BLACK);
g.fillOval(752, 297, 15, 15);
g.setColor(Color.RED);
g.fillRect(700, 480, 285, 75);
g.fillRect(985, 497, 145, 45);
g.fillRect(1130, 497, 30, 590);
g.setColor(Color.WHITE);
g.fillRect(702, 480, 15, 20);
g.fillRect(719, 480, 15, 20);
g.fillRect(736, 480, 15, 20);
g.fillRect(753, 480, 24, 20);
g.fillRect(779, 480, 24, 20);
g.fillRect(805, 480, 24, 20);
g.fillRect(702, 535, 15, 20);
g.fillRect(719, 535, 15, 20);
g.fillRect(736, 535, 15, 20);
g.fillRect(753, 535, 24, 20);
g.fillRect(779, 535, 24, 20);
g.fillRect(805, 535, 24, 20);
if (chewpressed == true) {
Color skint = new Color(255, 204, 133);
g.setColor(skint);
g.fillArc(810, 1000, 550, 550, 0, 180);
g.fillRect(1085, 600, 150, 550);
g.fillRect(700, 250, 285, 380);
int[] wt = {700, 700, 660};
int[] tw = {340, 420, 420};
g.fillPolygon(w, t, 3);
Color hairt = new Color(100, 60, 0);
g.setColor(hairt);
g.fillArc(700, 175, 535, 150, 0, 180);
g.setColor(Color.WHITE);
g.fillOval(730, 275, 45, 45);
g.setColor(Color.BLACK);
g.fillOval(752, 297, 15, 15);
g.setColor(Color.RED);
g.fillRect(700, 480, 285, 42);
g.fillRect(985, 497, 145, 35);
g.fillRect(1130, 497, 30, 590);
g.setColor(Color.WHITE);
g.fillRect(702, 480, 15, 20);
g.fillRect(719, 480, 15, 20);
g.fillRect(736, 480, 15, 20);
g.fillRect(753, 480, 24, 20);
g.fillRect(779, 480, 24, 20);
g.fillRect(805, 480, 24, 20);
g.fillRect(702, 502, 15, 20);
g.fillRect(719, 502, 15, 20);
g.fillRect(736, 502, 15, 20);
g.fillRect(753, 502, 24, 20);
g.fillRect(779, 502, 24, 20);
g.fillRect(805, 502, 24, 20);
repaint();
}
}
public void actionPerformed(ActionEvent e) {//ActionPerformed
if (chew.getText().equals("Press to chew the food")) {//if "press my belly" pressed,
chewpressed = true;//change boolean
repaint();
if (next.getText().equals("Press to go to the next step of digestion")) {
nextpressed = true;
}
repaint();
}
}
public void stateChanged(ChangeEvent e) {
int location1 = saliva.getValue();
}
}
//draw the info
//if buttonpressed is true
//show head with teeth clenched
//else if slidermoved is true
//show saliva moving in mouth
//else if nextbuttonpressed is true
//go to next panel
//ActionPerformed
//if button is pressed
//buttonpressed is true
//else if slider is moved
//slidermoved is true
//else if next button is pressed
//nextbuttonpressed is true
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));