Related
I'm creating a dots and boxes program and I am trying to feed the coordinate values from the main method where the user inputs them to the paintComponent method in the JFrame class. However I have to throw in the (Graphics g) parameter, and I don't see a way around it to feed in the values. It's probably big cringe because I'm still starting out but any help would be great.
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
JFrame f = new JFrame("Dots & Boxes");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Drawing a = new Drawing();
f.add(a);
f.setSize(1440,990);
f.setVisible(true);
Scanner input = new Scanner(System.in);
System.out.println("You will choose two coordinates on the dot grid to place a line between.");
System.out.println("Make sure that they are right next to each other, either vertically or horizontally (not diagonal)");
int xOne;
int yOne;
int xTwo;
int yTwo;
boolean playerOneTurn = true;
for (int i = 0; i <= 760; i++){
System.out.println("Pick Your First X-Coordinate: ");
xOne = input.nextInt();
System.out.println("Pick Your First Y-Coordinate: ");
yOne = input.nextInt();
System.out.println("Pick Your Second X-Coordinate: ");
xTwo = input.nextInt();
System.out.println("Pick Your Second Y-Coordinate: ");
yTwo = input.nextInt();
playerOneTurn = !playerOneTurn;
}
}
}
class Drawing extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
setFont(new Font("TimesRoman", Font.PLAIN, 20));
g.drawString("0", 75, 45);
g.drawString("1", 110, 45);
g.drawString("2", 145, 45);
g.drawString("3", 180, 45);
g.drawString("4", 215, 45);
g.drawString("5", 250, 45);
g.drawString("6", 285, 45);
g.drawString("7", 320, 45);
g.drawString("8", 355, 45);
g.drawString("9", 390, 45);
g.drawString("10", 417, 45);
g.drawString("11", 452, 45);
g.drawString("12", 487, 45);
g.drawString("13", 522, 45);
g.drawString("14", 557, 45);
g.drawString("15", 592, 45);
g.drawString("16", 627, 45);
g.drawString("17", 662, 45);
g.drawString("18", 697, 45);
g.drawString("19", 732, 45);
g.drawString("0", 40, 75);
g.drawString("1", 40, 110);
g.drawString("2", 40, 145);
g.drawString("3", 40, 180);
g.drawString("4", 40, 215);
g.drawString("5", 40, 250);
g.drawString("6", 40, 285);
g.drawString("7", 40, 320);
g.drawString("8", 40, 355);
g.drawString("9", 40, 390);
g.drawString("10", 35, 425);
g.drawString("11", 35, 460);
g.drawString("12", 35, 495);
g.drawString("13", 35, 530);
g.drawString("14", 35, 565);
g.drawString("15", 35, 600);
g.drawString("16", 35, 635);
g.drawString("17", 35, 670);
g.drawString("18", 35, 705);
g.drawString("19", 35, 740);
int dotx1 = 80;
int doty1 = 70;
((Graphics2D) g).setStroke(new BasicStroke(5));
for (int h = 0; h <= 19; h++) {
for (int i = 0; i <= 19; i++) {
g.drawLine(dotx1, doty1, dotx1, doty1);
dotx1 = dotx1 + 35;
}
dotx1 = 80;
doty1 = doty1 + 35;
}
}
}
You shouldn't call paintComponent by yourself. The method is called when Java is drawing the window.
The reason is that the user shouldn't care about re-drawing, most of the time. Java will decide when to redraw, for example on window minimize/maximize or resizing or when an element is clicked.
To draw animated shapes you must ask Java to redraw your window. You can add f.revalidate() to force components to redraw. Take a look at the documentation here.
Be really careful when using this. Never call this inside a loop without waiting between frames, because your CPU will go crazy if not !
If you want animated shapes, call revalidate() at fixed rate, for example 60fps.
In your case, you could simple put your Scanner inside the loop, ask the user for input, process it and then redraw the window.
Also, revalidate() is not blocking, it will just tell Java that the components tree has changed and must be redraw. But it will do it when he will have the time to do it.
Edit : As we are discussing in commentary, to feed your Drawing with the data to draw you must give him the informations.
First, create your graphical elements, let start with points :
class Point {
public float x, y;
Point(float x, float y) {
this.x = x;
this.y = y;
}
}
In your Drawing (who is in fact your Scene, let rename it), add setters and List
class Scene {
private List<Point> mPoints = new LinkedList<Point>();
public addPoint(Point p) {
mPoints.add(p);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
....
for (Point p : mPoints) {
// draw your points here
}
....
}
}
There several way to improve this simple example. First, you could add a paint(Graphics g) method on Point class.
When you will add another kind of component, you should create a base class Item and extend it in your graphical items (Point, Box...) and put the paint method in this new base class.
It's the way to don't have one list for each kind of element, and then you can just iterate over items and call paint without worrying about the kind behind it (Point, Box...)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've gone through and made classes for my objects and calls to them in my paint component, but I cant actually get them to move. Here is my move and check walls program that is in the object class:
public void move()
{
ballX += dx;
ballY += dy;
checkWalls();
}
//bounce of top/bottom, pass through left/right
public void checkWalls() {
//left-right
if(ballX > w) {
ballX = -diam;
}
else if(ballX < -diam) {
ballX = w;
}
//top-bottom
if(ballY < 0) {
dy *= -1;
}
else if(ballY + diam > h) {
dy *= -1;
}
}
And here is my call to them:
while(true) // infinite loop
{
jelly1.move();
frame.repaint();
try
{
Thread.sleep(10);
}
catch(Exception e){}
}
Also i feel the need to mention i have a background and a background component. The while(true) is in the background component because that's where the objects are created. And the frame is set visible in the background where the main method is.
Paint component is as follows:
public class BackgroundComponent extends JComponent {
Jellyfish jelly1;
Jellyfish jelly2;
Jellyfish jelly3;
Diver diver;
public BackgroundComponent() {
diver = new Diver(100, 300);
jelly1 = new Jellyfish(450, 450);
jelly2 = new Jellyfish(150, 300);
jelly3 = new Jellyfish(350, 75);
diver = new Diver(100, 300);
}
public void paintComponent(Graphics g){
//Drawing instructions go here
//Recover Graphics2D
Graphics2D g2 = (Graphics2D)g;
//Make gradient
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
int w = getWidth();
int h = getHeight();
Color color1 = Color.CYAN;
Color color2 = Color.BLACK;
GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
g2.setPaint(gp);
g2.fillRect(0, 0, w, h);
//Constructs rectangles on edge of screen and draws them
Rectangle box = new Rectangle(0,0,75,700);
g.setColor(Color.LIGHT_GRAY);
g2.fill(box);
Rectangle box2 = new Rectangle(625, 0, 75, 700);
g.setColor(Color.LIGHT_GRAY);
g2.fill(box2);
//Draws lines, with a stroke of 5, over rectangles
Line2D.Double segment = new Line2D.Double(10, 0, 10, 700);
g2.setStroke(new BasicStroke(5));
g.setColor(Color.GRAY);
g2.draw(segment);
Line2D.Double segment2 = new Line2D.Double(30, 0, 30, 700);
g.setColor(Color.GRAY);
g2.draw(segment2);
Line2D.Double segment3 = new Line2D.Double(50, 0, 50, 700);
g.setColor(Color.GRAY);
g2.draw(segment3);
Line2D.Double segment4 = new Line2D.Double(70, 0, 70, 700);
g.setColor(Color.GRAY);
g2.draw(segment4);
Line2D.Double segment5 = new Line2D.Double(690, 0, 690, 700);
g.setColor(Color.GRAY);
g2.draw(segment5);
Line2D.Double segment6 = new Line2D.Double(670, 0, 670, 700);
g.setColor(Color.GRAY);
g2.draw(segment6);
Line2D.Double segment7 = new Line2D.Double(650, 0, 650, 700);
g.setColor(Color.GRAY);
g2.draw(segment7);
Line2D.Double segment8 = new Line2D.Double(630, 0, 630, 700);
g.setColor(Color.GRAY);
g2.draw(segment8);
//Draws rectangle around title with thick boarder
Rectangle box3 = new Rectangle(40,40,620,75);
g.setColor(Color.WHITE);
g2.setStroke(new BasicStroke(5));
g2.draw(box3);
//Drawing text
String title = "Through the Depths";
//Sets font, font size, and color
g.setFont(new Font("Purisa", Font.BOLD, 50));
g.setColor(Color.DARK_GRAY);
g2.drawString(title, (50), 100);
//Places same text slightly up and over
g.setFont(new Font("Purisa", Font.BOLD, 50));
g.setColor(Color.WHITE);
g2.drawString(title, 53, 97);
//Draws ellipses with a stroke of 2 (these are the bubbles)
Ellipse2D.Double ellipse = new Ellipse2D.Double(450, 200, 150, 150);
g2.setStroke(new BasicStroke(2));
g2.draw(ellipse);
Ellipse2D.Double ellipse2 = new Ellipse2D.Double(510, 375, 90, 90);
g2.draw(ellipse2);
Ellipse2D.Double ellipse3 = new Ellipse2D.Double(470, 485, 70, 70);
g2.draw(ellipse3);
Ellipse2D.Double ellipse4 = new Ellipse2D.Double(510, 580, 45, 45);
g2.draw(ellipse4);
// Draws curves for bubbles
QuadCurve2D q = new QuadCurve2D.Float();
q.setCurve(548, 210, 607, 240, 590, 295);
g2.setStroke(new BasicStroke(3));
g2.draw(q);
QuadCurve2D q2 = new QuadCurve2D.Float();
q2.setCurve(575, 387, 607, 415, 585, 445);
g2.draw(q2);
QuadCurve2D q3 = new QuadCurve2D.Float();
g2.setStroke(new BasicStroke(2));
q3.setCurve(515, 493, 545, 511, 528, 540);
g2.draw(q3);
QuadCurve2D q4 = new QuadCurve2D.Float();
g2.setStroke(new BasicStroke(1));
q4.setCurve(538, 585, 558, 595, 545, 617);
g2.draw(q4);
// Sets color to pink before drawing jellyfish
g.setColor(Color.PINK);
//draws jellyfish
jelly1.draw(g);
jelly2.draw(g);
jelly3.draw(g);
// Draws diver
diver.draw(g);
while(true) // infinite loop
{
jelly1.move();
repaint();
try
{
Thread.sleep(10);
}
catch(Exception e){}
}
}
}
while(true){ ... } and Thread.Sleep inside a paintComponent implementation is completely the wrong way of doing things. By doing this, you are blocking the Event Dispatching Thread completely which means that your UI will no longer be updated properly and the UI will no longer be responsive.
What you should do:
Remove the while(true){ ... } loop from the paintComponent override
Create a javax.swing.Timer, set it to run every 10 milliseconds
In the ActionListener.actionPerformed implementation - the action that will be performed each 10 ms - move the jelly and call for a repaint
Start this timer after initialization is done, eg at the end of your constructor or initialization method (not paintComponent).
Stop the timer when it is no longer needed
Simplified example for this Timer, based on your snippet:
new javax.swing.Timer( 10, new ActionListener( ) {
#override
public void actionPerformed( ActionEvent e ) {
jelly1.move();
repaint();
}
}).start();
//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)
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).