1

I am making a Yahtzee dice roll program using JCreator. For class I was instructed to use images in the project. I want to display a png with the Yahtzee title the whole time while the applet runs. But once a button is clicked in the applet, the image disappears. Below is only part of my main class code since I do not think I need to post all of it. I figure its just an issue in my paint method. Thank you

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class DB extends JApplet {
private JPanel P;
private JPanel buttonpanel;
private JButton reset;
private JButton wildcat;
private JButton hold1;
private JButton hold2;
private JButton hold3;
private JButton hold4;
private JButton hold5;
private int x; 
private boolean holda=true;
private boolean holdb=true;
private boolean holdc=true;
private boolean holdd=true;
private boolean holde=true;
public static int dice1y=250; 
public static int dice2y=250; 
public static int dice3y=250; 
public static int dice4y=250; 
public static int dice5y=250; 
private int turn=3;

public void init() {
    P=new P();

    add(P,BorderLayout.CENTER);
    buildbuttons();
    add(buttonpanel, BorderLayout.SOUTH);
}
public void buildbuttons(){
    buttonpanel = new JPanel();
    buttonpanel.setSize(400,50);
    wildcat= new JButton ("Roll");
    reset= new JButton ("reset");
    hold1= new JButton ("Hold");
    hold2= new JButton ("Hold");
    hold3= new JButton ("Hold");
    hold4= new JButton ("Hold");
    hold5= new JButton ("Hold");
    buttonpanel.setLayout(new GridLayout(1,7));
    buttonpanel.add(wildcat);
    buttonpanel.add(reset);
    buttonpanel.add(hold1);
    buttonpanel.add(hold2);
    buttonpanel.add(hold3);
    buttonpanel.add(hold4);
    buttonpanel.add(hold5);
    wildcat.addActionListener(new ButtonListener());
    reset.addActionListener(new ButtonListener());
    hold1.addActionListener(new ButtonListener());
    hold2.addActionListener(new ButtonListener());
    hold3.addActionListener(new ButtonListener());
    hold4.addActionListener(new ButtonListener());
    hold5.addActionListener(new ButtonListener());

}
public void reset(){
    turn=3;
    dice1y=250;
    dice2y=250;
    dice3y=250;
    dice4y=250;
    dice5y=250;
 holda=true;
 holdb=true;
 holdc=true;
 holdd=true;
 holde=true;


}   



public void paint(Graphics g) {

    super.paint(g);


        g.drawImage(getImage(getDocumentBase(),"Yah copy.PNG"),25,25,420,100,this);


}
private class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){

        if(e.getSource()==reset){
            reset();
}
        if(e.getSource()==hold1){
        holda = !holda; 
            if(holda==false){  .............//half way point

https://i.stack.imgur.com/2t9rZ.png

https://i.stack.imgur.com/Blttk.png

Mitchell Carroll
  • 479
  • 5
  • 13
dddd
  • 11
  • 1
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). .. – Andrew Thompson May 03 '15 at 03:57
  • .. 4) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 5) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson May 03 '15 at 03:57

2 Answers2

0

If you want a particular image to be there until the applet runs then keep a separate panel for it, and don't give commands for it in code of a button.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
Chetas
  • 1
  • 1
0

Since you're not using Swing, you need also to override the update() method in a similar way to paint(). This is because when the runtime environment makes a call to repaint(), a call is made to paint() if the component is lightweight (belongs to Swing) or to update() if the component is heavyweight (belongs to AWT). See http://www.oracle.com/technetwork/java/painting-140037.html

Mitchell Carroll
  • 479
  • 5
  • 13