Related
I am writing a program simulating a single round of the card game WAR.
I don't know how to graphically display the card (number/color/suit).
import java.util.*; // for Scanner class
import java.awt.*;
import javax.swing.*;
// Class Definition
class Project4 {
// Main method definition
public static void main(String[] args) {
// Object and Variable Declarations
JFrame win;
Container contentPane;
Graphics g;
String player1Name, player2Name, scoreWinLoss;
//This is the graphics portion, all of the graphics (so far) are working.
win = new JFrame("War Game");
win.setSize(900, 600);
win.setLocation(100, 100);
win.setVisible(true);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color white = new Color(255, 255, 255);
Color white2 = new Color(255, 255, 255);
Color black = new Color(0, 0, 0);
Color black2 = new Color(0, 0, 0);
contentPane = win.getContentPane();
//Also if anyone knows how to change this .setBackground(Color.Green)from lime green to forest(card table) green that would be awesome!
contentPane.setBackground(Color.GREEN);
g = contentPane.getGraphics();
Font font = new Font("Serif", Font.BOLD, 25);
g.setFont(font);
try {
Thread.sleep(200);
} catch (Exception e) {}
player1Name = JOptionPane.showInputDialog(null, "Player 1, what is your name?");
player2Name = JOptionPane.showInputDialog(null, "Player 2, what is your name?");
g.setColor(black);
g.drawString(player1Name, 220, 35);
g.setColor(black2);
g.drawString(player2Name, 625, 35);
g.setColor(white);
g.fillRoundRect(125, 50, 250, 450, 20, 20);
g.setColor(white2);
g.fillRoundRect(525, 50, 250, 450, 20, 20);
g.setColor(black);
g.drawRoundRect(125, 50, 250, 450, 20, 20);
g.setColor(black2);
g.drawRoundRect(525, 50, 250, 450, 20, 20);
//This is where it gets confusing and this stuff is all not working
int card1Max = 14, card1Min = 2, card2Max = 14, card2Min = 2, randomNum1 = 0, randomNum2 = 0;
g.setColor(black);
g.drawInt(randomNum1, 150, 35);
g.setColor(black2);
g.drawInt(randomNum2, 500, 35);
Random randomPicker = new Random(System.currentTimeMillis());
randomNum1 = randomPicker.nextInt(card1Max - card1Min + 1) + card1Min;
randomNum2 = randomPicker.nextInt(card2Max - card2Min + 1) + card2Min;
if (randomNum1 < randomNum2) {
scoreWinLoss = player2Name + " won!";
} else if (randomNum1 > randomNum2) {
scoreWinLoss = player1Name + " won!";
} else if (randomNum1 == randomNum2) {
scoreWinLoss = "It's a tie! Play again to determine a winner.";
}
Do not play with the graphics object like you are doing :
g.setColor(black);
g.drawString(player1Name, 220, 35);
This is bad!
Instead you should look at a layout. Look at GridLayout, or GridBagLayout. These allow you to organise JPanels as a grid. With these panels you can then set the background color and add text (via a JLabel).
if i understand your problem: as simple way, you can use JPanel with CardLayout and then find card picture from web then, with JLabel.setImageIcon(ImageIcon imageIcon) method of JLabel set this pictures on it and by CardLayout bring up cards on toghether.
//ADDING BET FIELDS
setLayout(null);
horseChoice = new JTextField();
horseChoice.setBounds(200, 585, 300, 20);
add(horseChoice);
setLayout(null);
horseBet = new JTextField();
horseBet.setBounds(200, 625, 300, 20);
add(horseBet);
//ADDING BET BUTTON
setLayout(null);
bet = new JButton("Place Bet");
bet.setBounds(250, 675, 200, 50);
add(bet);
I am trying to create two textfields where the users can type in what they want and a button to save their input to a variable. But when I run my program, the text fields and button flash and then become invisible. Then if the user clicks on them they flash again. How do I stop this from happening?
The rest of the class:
// the "main" function
public void run(){
init();
font = new Font ("Arial", Font.BOLD, 14);
titleFont = new Font ("Comic Sans MS", Font.BOLD, 20);
textColor = new Color(100, 100, 100);
resultColor = new Color(255, 165, 0, 180);
//ADDING HORSES
horse1 = new Horses_1();
horse2 = new Horses_2();
horse3 = new Horses_3();
horse4 = new Horses_4();
//GAMELOOP
while (running){
start = System.nanoTime();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
try{
Thread.sleep(wait);
}
catch(Exception e){
e.printStackTrace();
}
//TIMER
if(horse1.h1x < 1350){
horse1Time++;
}
if(horse2.h2x < 1350){
horse2Time++;
}
if(horse3.h3x < 1350){
horse3Time++;
}
if(horse4.h4x < 1350){
horse4Time++;
}
//STOP RACING TIMERS
if(horse1Result == true && horse1.h1x >= 1350){
horse1TimeStr = horse1Time + ", Dominic";
horse1Result = false;
}
if(horse2Result == true && horse2.h2x >= 1350){
horse2TimeStr = horse2Time + ", Charlie";
horse2Result = false;
}
if(horse3Result == true && horse3.h3x >= 1350){
horse3TimeStr = horse3Time + ", Admiral";
horse3Result = false;
}
if(horse4Result == true && horse4.h4x >= 1350){
horse4TimeStr = horse4Time + ", Bacardi";
horse4Result = false;
}
//RANDOM HORSE CHOICE & MOVE PLACES
if(horse1.h1x >= 1350){
final int[] CHOICES = { 2,3,4 };
}
else if(horse2.h2x >= 1350){
final int[] CHOICES = { 1,3,4 };
}
else if(horse3.h3x >= 1350){
final int[] CHOICES = { 1,2,4 };
}
else if(horse4.h4x >= 1350){
final int[] CHOICES = { 1,2,3 };
}
else{
final int[] CHOICES = { 1,2,3,4 };
}
randomHorse = CHOICES[rand.nextInt(CHOICES.length)];
randomMove = rand.nextInt(35) + 1;
//PLACES
Update();
Draw();
drawToScreen();
}
}
// updates the game
private void Update() {
//UPDATE RACE COUNTER
if(horse1.h1x == 100 && horse2.h2x == 100 && horse3.h3x == 100 && horse4.h4x == 100){
races++;
}
//HORSE UPDATE
horse1.Update();
horse2.Update();
horse3.Update();
horse4.Update();
//RESULT SETTING
if(result == true && horse1Result == false && horse2Result == false && horse3Result == false && horse4Result == false){
String[] horses =
{ horse1TimeStr, horse2TimeStr, horse3TimeStr, horse4TimeStr };
Arrays.sort(horses);
firstHorse = horses[0];
secondHorse = horses[1];
thirdHorse = horses[2];
fourthHorse = horses[3];
firstHorse = firstHorse.replaceAll("[0-9,,]","");
secondHorse = secondHorse.replaceAll("[0-9,,]","");
thirdHorse = thirdHorse.replaceAll("[0-9,,]","");
fourthHorse = fourthHorse.replaceAll("[0-9,,]","");
result = false;
}
}
// draws the game onto an off-screen buffered image
private void Draw() {
//BACKGROUND COLOUR
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
if(button == true){
//ADDING BET FIELDS
setLayout(null);
horseChoice = new JTextField();
horseChoice.setBounds(200, 585, 300, 20);
add(horseChoice);
setLayout(null);
horseBet = new JTextField();
horseBet.setBounds(200, 625, 300, 20);
add(horseBet);
//ADDING BET BUTTON
setLayout(null);
bet = new JButton("Place Bet");
bet.setBounds(250, 675, 200, 50);
add(bet);
button = false;
}
horse1.Draw(g);
horse2.Draw(g);
horse3.Draw(g);
horse4.Draw(g);
//HORSE 1 - TRACK
g.setColor(new Color (255, 0, 0));
g.drawLine(100, 125, WIDTH - 100, 125);
//HORSE 2 - TRACK
g.setColor(new Color (0, 255, 0));
g.drawLine(100, 225, WIDTH - 100, 225);
//HORSE 3 - TRACK
g.setColor(new Color (0, 0, 255));
g.drawLine(100, 325, WIDTH - 100, 325);
//HORSE 4 - TRACK
g.setColor(new Color (255, 255, 0));
g.drawLine(100, 425, WIDTH - 100, 425);
//FINISH LINE
g.setColor(new Color (255, 255, 0));
g.drawLine(WIDTH - 100, 475, WIDTH - 100, 375);
g.setColor(new Color (0, 0, 255));
g.drawLine(WIDTH - 100, 275, WIDTH - 100, 375);
g.setColor(new Color (0, 255, 0));
g.drawLine(WIDTH - 100, 175, WIDTH - 100, 275);
g.setColor(new Color (255, 0, 0));
g.drawLine(WIDTH - 100, 75, WIDTH - 100, 175);
//HUD
g.setColor(new Color (180, 180, 180));
g.drawLine(50, 550, WIDTH - 50, 550);
g.drawLine(550, 550, 550, 700);
g.drawLine(900, 550, 900, 700);
g.setColor(resultColor);
g.setFont(titleFont);
g.drawString("Results", 700, 540);
g.drawString("Details", 1000, 540);
g.drawString("Betting", 360, 540);
//Results
g.setColor(textColor);
g.setFont(font);
g.drawString("Winner : ", 600, 600);
g.drawString("Second : ", 600, 630);
g.drawString("Third : ", 600, 660);
g.drawString("Fourth : ", 600, 690);
//DETAILS
g.drawString("Race Number :", 950, 600);
g.drawString("Horse Choice :", 950, 630);
g.drawString("Bet :", 950, 660);
g.setColor(resultColor);
g.drawString("" + races, 1250, 600);
//BETS
g.setColor(textColor);
g.drawString("Horse Choice :", 75, 600);
g.drawString("Bet :", 75, 640);
if(horse1Result == false && horse2Result == false && horse3Result == false && horse4Result == false){
g.setColor(resultColor);
g.drawString(" " + firstHorse, 780, 600);
g.drawString(" " + secondHorse, 780, 630);
g.drawString(" " + thirdHorse, 780, 660);
g.drawString(" " + fourthHorse, 780, 690);
}
g.setColor(textColor);
g.setFont(font);
//HORSE 1 TEXT
String h1 = "Dominic -";
g.drawString(h1, 10, 130);
//HORSE 2 TEXT
String h2 = "Charlie -";
g.drawString(h2, 10, 230);
//HORSE 3 TEXT
String h3 = "Admiral -";
g.drawString(h3, 10, 330);
//HORSE 4 TEXT
String h4 = "Bacardi -";
g.drawString(h4, 10, 430);
}
Looks like while(running) and Thread.sleep is giving your problems. For Swing apps use a javax.swing.Timer. Here is the basic construct
Timer(int delay, ActionListener listener)
where delay is the time to be delayed ("slept") between intervals and the listener is used just like a listener for a button, except you don't need to press a button for the event to fire, the Timer fires the event every delayed milliseconds. You can do something like this
Timer timer = new Timer(delay, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
// the stuff in your while loop here
}
});
timer.starts();
Here's an example of a Timer being used. Take a look at the docs for more methods you can use with the timer.
Side Note
It's not recommended to use a null layout. Take a look at Laying out Components within a Container for details on how to use Layout Managers, as you should be using instead of a null layout.
I'm new to Java so thank you for your time and help!
Basically I want to get text to display over a graphic I have created. Basically I made a trophy and want it to display "#1" on it.
Can anyone please help me with how to do this? Thank you again
Below is the code I have written so far. You can see that the position for the "#1" is all set and done but it's appearing behind the graphic.
import javax.swing.JApplet;
import java.awt.*;
import java.net.*;
import javax.imageio.*;
import java.io.*;
import java.awt.Graphics2D;
public class BusinessCard extends JApplet
{
/**
* Paint method for applet.
*
* #param g the Graphics object for this applet
*/
public void paint(Graphics page)
{
//Variables used in rectangle
int x = 0;
int y = 0;
int width = 500;
int height = 300;
page.drawRect(x, y, width, height); //draws the rectangle using variables
//Displays name
Font f = new Font("Helvetica", Font.BOLD, 26);
page.setFont(f);
page.drawString ("anonymous", 300,100);
//Displays company
Font g = new Font("Helvetica", Font.PLAIN, 18);
page.setFont(g);
page.drawString ("blank", 320, 120);
//Displays email
Font h = new Font("Helvetica", Font.PLAIN, 15);
page.setFont(h);
page.drawString ("email", 315,140);
//int for the logo
final int MID = 350;
final int TOP = 168;
setBackground (Color.yellow); //Revisit. For some reason it only turns yellow after you resize the window
Font i = new Font("Helvetica", Font.BOLD, 16);
page.setFont(i);
page.drawString ("#1", MID+20, TOP+20);
page.setColor (Color.orange);
page.fillOval (MID, TOP, 60, 60); //bottom half of the trophy. the rounded part.
page.drawArc (MID-8, TOP+15, 25, 25, 100, 160); //left arc
page.drawArc (MID+43, TOP+15 , 25, 25, 280, 160); //right arc
page.fillRect (MID+1, TOP+1, 59, 25); //make the top of the trophy flat basically
page.fillRect (MID+22, TOP+60, 15, 25); //neck of the trophy
page.drawLine (MID+48, TOP+84, MID+10, TOP+84); //base of the trophy
}
I think you have to draw the trophy first and then write the text!
Whatever you draw appears on the top layer of the control.
First, do what sabre_raider said. You do want it before, but something else must be fixed too
You're drawing orange on orange. You draw the trophy, then set the background of your page to be yellow, then draw the "#1" in orange again. Change setBackground to page.setColor to draw in yellow (which appears to be what you want). setBackground sets the background of the applet, which im assuming is not what you want, but rather to draw the "#1" in yellow. If you did mean to set the background, make sure to do that in Applet's init method!! Not each time you repaint. (Along with that, in init, have setSize(500, 300) to properly size the window to the size of your message)
Also, I recomend splitting that up into different methods, as that will just make it more readable. Here's what your methods should look like in general:
/**
* Paint method for applet.
*
* #param g
* the Graphics object for this applet
*/
public void paint(Graphics page)
{
drawBorder( page );
drawText( page );
drawTrophy( page, 1 );
}
private void drawBorder(Graphics page) {
int x = 0;
int y = 0;
int width = 500;
int height = 300;
page.setColor( Color.black );
page.drawRect( x, y, width, height );
}
private void drawText(Graphics page) {
// Displays name
Font f = new Font( "Helvetica", Font.BOLD, 26 );
page.setFont( f );
page.drawString( "anonymous", 300, 100 );
// Displays company
Font g = new Font( "Helvetica", Font.PLAIN, 18 );
page.setFont( g );
page.drawString( "blank", 320, 120 );
// Displays email
Font h = new Font( "Helvetica", Font.PLAIN, 15 );
page.setFont( h );
page.drawString( "email", 315, 140 );
}
private void drawTrophy(Graphics page, int number) {
final int MID = 350;
final int TOP = 168;
page.setColor( Color.orange );
page.fillOval( MID, TOP, 60, 60 ); // bottom half of the trophy. the rounded part.
page.drawArc( MID - 8, TOP + 15, 25, 25, 100, 160 ); // left arc
page.drawArc( MID + 43, TOP + 15, 25, 25, 280, 160 ); // right arc
page.fillRect( MID + 1, TOP + 1, 59, 25 ); // make the top of the trophy flat basically
page.fillRect( MID + 22, TOP + 60, 15, 25 ); // neck of the trophy
page.drawLine( MID + 48, TOP + 84, MID + 10, TOP + 84 ); // base of the trophy
Font i = new Font( "Helvetica", Font.BOLD, 16 );
page.setColor( Color.yellow );
page.setFont( i );
page.drawString( "#" + number, MID + 20, TOP + 20 );
}
Firstly, I wouldn't paint directly onto a top level container. I'd use something like a JPanel to do my custom painting and then add that to the top level container.
Secondly, you should avoid overriding paint where possible. If you use a custom component as suggested in the first point, you should use paintComponent instead.
Thirdly, ALWAYS call super.paint (or super.paintComponent if you're using a custom component).
Fourthly, ever body else is right. You should be trying to draw you text AFTER the graphics...
This is the code I used:
public class BusinessCard extends JApplet {
/**
* Paint method for applet.
*
* #param g the Graphics object for this applet
*/
public void paint(Graphics page) {
super.paint(page);
//Variables used in rectangle
int x = 0;
int y = 0;
int width = 500;
int height = 300;
page.drawRect(x, y, width, height); //draws the rectangle using variables
//int for the logo
final int MID = 350;
final int TOP = 168;
page.setColor(Color.orange);
page.fillOval(MID, TOP, 60, 60); //bottom half of the trophy. the rounded part.
page.drawArc(MID - 8, TOP + 15, 25, 25, 100, 160); //left arc
page.drawArc(MID + 43, TOP + 15, 25, 25, 280, 160); //right arc
page.fillRect(MID + 1, TOP + 1, 59, 25); //make the top of the trophy flat basically
page.fillRect(MID + 22, TOP + 60, 15, 25); //neck of the trophy
page.drawLine(MID + 48, TOP + 84, MID + 10, TOP + 84); //base of the trophy
page.setColor(Color.yellow); //Revisit. For some reason it only turns yellow after you resize the window
Font font = UIManager.getFont("Label.font");
page.setFont(font.deriveFont(Font.BOLD, 16));
page.drawString("#1", MID + 20, TOP + 20);
//Displays name
page.setFont(font.deriveFont(Font.BOLD, 26));
page.drawString("anonymous", 300, 100);
//Displays company
page.setFont(font.deriveFont(Font.PLAIN, 18));
page.drawString("blank", 320, 120);
//Displays email
page.setFont(font.deriveFont(Font.PLAIN, 15));
page.drawString("email", 315, 140);
}
}
And it produced
Beware, that everybody may have the font you're trying to use installed (you code caused the applet to crash)
import javax.swing.JApplet;
import java.awt.*;
public class Snowman extends JApplet {
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
public void paint (Graphics page)
{
final int MID = 150;
final int TOP = 50;
setBackground (Color.cyan);
page.setColor(Color.blue);
page.fillRect(0, 175, 300, 50); // ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40); // head
page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
page.fillOval (MID-50, TOP+80, 100, 60); // lower torso
page.setColor (Color.black);
page.fillOval(MID-10, TOP+10, 5, 5);
page.fillOval(MID+5, TOP+10, 5, 5);
page.drawArc(MID-10, TOP+20, 20, 10, 190, 160); // smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect(MID-15, TOP-20, 30, 25); // top of hat
}
}
This is all the code. The setBackground is stated after I declare the two final variables, thanks in advance, I got this code from a book, "Java Software Solutions", I looked over it over and over, and no luck :/ thanks in advance :)
//<applet code='Snowman' width=300 height=200></applet>
import javax.swing.*;
import java.awt.*;
public class Snowman extends JApplet {
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
public void init() {
add(new SnowmanPanel());
validate();
}
}
class SnowmanPanel extends JPanel {
final int MID = 150;
final int TOP = 50;
SnowmanPanel() {
setBackground (Color.cyan);
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.setColor(Color.blue);
page.fillRect(0, 175, 300, 50); // ground
page.setColor (Color.yellow);
page.fillOval (-40, -40, 80, 80); // sun
page.setColor (Color.white);
page.fillOval (MID-20, TOP, 40, 40); // head
page.fillOval (MID-35, TOP+35, 70, 50); // upper torso
page.fillOval (MID-50, TOP+80, 100, 60); // lower torso
page.setColor (Color.black);
page.fillOval(MID-10, TOP+10, 5, 5);
page.fillOval(MID+5, TOP+10, 5, 5);
page.drawArc(MID-10, TOP+20, 20, 10, 190, 160); // smile
page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm
page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm
page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat
page.fillRect(MID-15, TOP-20, 30, 25); // top of hat
}
}
General advice.
Don't paint in a top-level Swing component. Instead move the custom painting into a JPanel or JComponent and paint there. For custom painting in the latter, override paintComponent(Graphics)
When doing custom painting, remember to call super.paintComponent(Graphics)
Set the color in the constructor (or init()) rather than in the paint method.
Other advice
For a static image, you can also draw it to a BufferedImage and put the image in an ImageIcon in a JLabel. Which is simpler.
If this book has rushed into creating applets, toss it away. applets are much more difficult than standard apps., and should not be attempted by newbies.
Try this code
import java.awt.*;
import javax.swing.JApplet;
public class SnowMan extends JApplet
{
public SnowMan()
{
setBackground(Color.cyan);
}
//---------------------------------------------
// Draws a snowman.
//---------------------------------------------
#Override
public void paint(Graphics page)
{
final int MID = 150;
final int TOP = 50;
page.setColor(Color.blue);
page.fillRect(0, 175, 300, 50); // ground
page.setColor(Color.yellow);
page.fillOval(-40, -40, 80, 80); // sun
page.setColor(Color.white);
page.fillOval(MID - 20, TOP, 40, 40); // head
page.fillOval(MID - 35, TOP + 35, 70, 50); // upper torso
page.fillOval(MID - 50, TOP + 80, 100, 60); // lower torso
page.setColor(Color.black);
page.fillOval(MID - 10, TOP + 10, 5, 5);
page.fillOval(MID + 5, TOP + 10, 5, 5);
page.drawArc(MID - 10, TOP + 20, 20, 10, 190, 160); // smile
page.drawLine(MID - 25, TOP + 60, MID - 50, TOP + 40); // left arm
page.drawLine(MID + 25, TOP + 60, MID + 55, TOP + 60); // right arm
page.drawLine(MID - 20, TOP + 5, MID + 20, TOP + 5); // brim of hat
page.fillRect(MID - 15, TOP - 20, 30, 25); // top of hat
}
}
setBackground (Color.cyan);
It is working properly in my IDE. I also changed the color of background.It works nice and properly. No need to change the code.Make sure when you are creating the class.
The paint(Graphics) method is used only to paint the parameter (in your case page).
The application applet background color has already been processed by this stage.
That is why you can fix the problem by setting it in the constructor:
public Snowman()
{
this.setBackground(Color.cyan);
}
I think you need to use, getContentPane().setBackground()
I'm making a Pinball style applet and I ran into some problems before that have thankfully been remedied. However, I've hit another stumbling block.
To draw flippers for the pinball machine, I intended to draw an angled rounded rectangle which would allow me to possibly animate it later on so that you could see it moving up when I add threads to the applet. This is all done in the Table's paintComponent method. When I don't apply any rotation to check that it's all working ok, it draws the shape fine. But when I apply a rotation (whether through an Affine Transform object as in my code now or the Graphics2D.rotate method) the shape does not draw for some reason. I'm not sure why but I'm hoping that I've merely overlooked something. I have looked around online, but either I'm not putting in the right keywords or it is simply that I'm overlooking something.
The code for my Pinball and Table classes is below, please feel free to point out anything that you think could cause this strangeness. Note that I haven't given the right coordinates exactly yet as I can tweek it's position when it's drawn and I can see it, in case any of you run any similar code to try and debug it.
As a P.S. Would calling the ball's constructor on table be a better idea than doing it on the applet? Just wondering if what I've done with that is really inefficient.
import javax.swing.*; // useful for the drawing side, also going to be a JApplet
import java.awt.*;
import java.net.*;
public class Pinball extends JApplet
{
// variables go here
Table table;
Player player;
Ball ball;
Miosoft miosoft;
Miofresh miofresh;
Mioboost mioboost;
Miocare miocare;
Miowipe miowipe;
// further initialisation of the GUI
public void init()
{
setSize(300, 300);
table = new Table(this);
player = new Player();
ball = new Ball(180, 175); // set the ball's initial position in the arguments
miosoft = new Miosoft();
miocare = new Miocare();
miowipe = new Miowipe();
miofresh = new Miofresh();
mioboost = new Mioboost();
setContentPane(table); // makes our graphical JPanel container the content pane for the Applet
// createGUI(); // this has been moved onto the table class
}
public void stop()
{
}
// little getters to make things easier on the table
public int getBallX()
{
return ball.getXPosition();
}
public int getBallY()
{
return ball.getYPosition();
}
public int getLives()
{
return player.getLives();
}
public int getScore()
{
return player.getScore();
}
}
And here is the table class
import java.awt.*; // needed for old style graphics stuff
import java.awt.geom.AffineTransform;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*; // gives us swing stuff
import sun.rmi.transport.LiveRef;
import java.io.IOException;
import java.net.*; // useful for anything using URLs
/*--------------------------------------------------------------
Auto-generated Java code for class Table
Generated by QSEE-SuperLite multi-CASE, QSEE-Technologies Ltd
www.qsee-technologies.com
Further developed to be the Content pane of this application by Craig Brett
----------------------------------------------------------------*/
public class Table extends JPanel
{
// attributes go here
Pinball pb;
Color bgColour;
Color launcherColour;
Color ballColour;
Image logo;
Image freshBonus;
Image midCircle;
Image leftBumper;
Image rightBumper;
Image nappy1;
Image nappy2;
Image nappy3;
int ballX;
int ballY;
int playerLives;
int playerScore;
// constructor goes here
public Table(Pinball pb)
{
setPreferredSize(new Dimension(300, 300));
this.pb = pb;
// this is not needed anymore, with the new loadImage class down the bottom
// Toolkit tk = Toolkit.getDefaultToolkit(); // needed to get images
// logo = tk.getImage(base + "images/bambinomio.jpg");
logo = loadImage("bambinomio.jpg");
freshBonus = loadImage("miofresh circle.jpg");
midCircle = loadImage("middle circle.jpg");
leftBumper = loadImage("left bumper.jpg");
rightBumper = loadImage("right bumper.jpg");
nappy1 = loadImage("nappy1.jpg");
nappy2 = loadImage("nappy2.jpg");
nappy3 = loadImage("nappy3.jpg");
createGUI();
}
// public methods go here
// all GUI creation stuff goes here
public void createGUI()
{
// setting the background colour
bgColour = new Color(190, 186, 221); // makes the sky blue colour for the background.
setBackground(bgColour);
launcherColour = new Color(130, 128, 193);
ballColour = new Color(220, 220, 220); // the color of the launch spring and ball
// setOpaque(false);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// to give us access to the 2D graphics features
Graphics2D g2d = (Graphics2D) g;
// creating the panels
g2d.setColor(Color.WHITE);
g2d.fillRect(200, 20, 100, 280); // the logo Panel
g2d.fillRect(0, 0, 300, 20);
g2d.setColor(Color.BLACK);
g2d.drawRoundRect(230, 125, 40, 120, 20, 20); // the lives panel
g2d.drawRoundRect(210, 255, 80, 40, 20, 20);
g2d.drawString("Score", 215, 270);
g2d.drawString(displayScore(), 215, 290);
// now drawing the graphics
g2d.drawImage(logo, 205, 25, 90, 90, null);
g2d.drawImage(freshBonus, 10, 40, 20, 20, this);
g2d.drawImage(leftBumper, 40, 200, 10, 20, this);
g2d.drawImage(rightBumper, 150, 200, 10, 20, this);
// now the three mid circles
g2d.drawImage(midCircle, 55, 120, 25, 25, this);
g2d.drawImage(midCircle, 95, 90, 25, 25, this);
g2d.drawImage(midCircle, 95, 150, 25, 25, this);
// now filling out the lives depending on how many the players have
playerLives = pb.getLives();
if(playerLives >= 1)
g2d.drawImage(nappy1, 235, 135, 32, 30, this);
if(playerLives >= 2)
g2d.drawImage(nappy2, 235, 170, 32, 30, this);
if(playerLives >= 3)
g2d.drawImage(nappy3, 235, 205, 32, 30, this);
// now to handle the white lines
g2d.setColor(Color.WHITE);
g2d.drawLine(20, 250, 20, 60); // the left edge
g2d.drawArc(10, 40, 20, 20, 45, 225); // the top left corner
g2d.drawLine(28, 43, 160, 43); // the top of the table
g2d.drawArc(150, 41, 40, 30, 0, 120); // the top right corner
g2d.drawLine(20, 250, 35, 270); // the bottom left corner
// now for the launcher, we draw here
g2d.setColor(launcherColour);
g2d.fillRoundRect(170, 75, 30, 175, 20, 20); // the blue tube
g2d.setColor(ballColour);
g2d.fillRoundRect(175, 210, 20, 40, 20, 20); // the first bit of the launcher
g2d.fillRect(175, 210, 20, 20); // the top of the launcher's firer
g2d.fillRoundRect(175, 195, 20, 10, 20, 20); // the bit that hits the ball
// now drawing the ball wherever it is
ballX = pb.getBallX();
ballY = pb.getBallY();
g2d.fillOval(ballX, ballY, 10, 10);
// now the flippers
g2d.setPaint(Color.WHITE);
// more precise coordinates can be given when the transformation below works
// RoundRectangle2D leftFlipper = new RoundRectangle2D.Double(43.0, 245.0, 20.0, 50.0, 30.0, 30.0); // have to make this using shape parameters
// now using Affine Transformation to rotate the rectangles for the flippers
AffineTransform originalTransform = g2d.getTransform();
AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(120));
g2d.transform(transform); // apply the transform
g2d.fillRoundRect(33, 260, 20, 40, 10, 10);
g2d.setTransform(originalTransform); // resetting the angle
// g2d.drawString("The flipper should be drawn", 80, 130); // for debugging if the previous draw instructions have been carried out
}
// a little useful method for handling loading of images and stuff
public BufferedImage loadImage(String filename)
{
BufferedImage image = null;
try
{
URL url = new URL(pb.getCodeBase(), "images/" + filename);
image = ImageIO.read(url);
}catch(IOException e)
{
System.out.println("Error loading image - " + filename + ".");
}
return image;
}
private String displayScore()
{
playerScore = pb.getScore();
String scoreString = Integer.toString(playerScore);
String returnString = "";
if(scoreString.length() < 8)
{
for(int i = 0; i < (8 - scoreString.length()); i++)
{
returnString = returnString + "0";
}
}
returnString = returnString + scoreString;
return returnString;
}
}
Normally to rotate something, you rotate it around some point of interest (the anchor point of the thing that is rotating), not the point 0,0. It appears you are rotating around 0, 0 so chances are this is getting rotated off the screen. To rotate around an arbitrary point, first translate that point to 0 (negative translation) then rotate, then do translate back (postive translation).