repaint problem - java

I have a problem with my repaint in the method move. I dont know what to doo, the code is below
import java.awt.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.*;
import java.lang.*;
public class bbb extends JPanel
{
public Stack<Integer> stacks[];
public JButton auto, jugar, nojugar;
public JButton ok, ok2;
public JLabel info = new JLabel("Numero de Discos: ");
public JLabel instruc = new JLabel("Presiona la base de las torres para mover las fichas");
public JLabel instruc2 = new JLabel("No puedes poner una pieza grande sobre una pequenia!");
public JComboBox numeros = new JComboBox();
public JComboBox velocidad = new JComboBox();
public boolean seguir = false, parar = false, primera = true;
public int n1, n2, n3;
public int click1 = 0;
public int opcion = 1, tiempo = 50;
public int op = 1, continuar = 0, cont = 0;
public int piezas = 0;
public int posx, posy;
public int no;
public bbb() throws IOException
{
stacks = new Stack[3];
stacks[0] = new Stack<Integer>();
stacks[1] = new Stack<Integer>();
stacks[2] = new Stack<Integer>();
setPreferredSize(new Dimension(1366, 768));
ok = new JButton("OK");
ok.setBounds(new Rectangle(270, 50, 70, 25));
ok.addActionListener(new okiz());
ok2 = new JButton("OK");
ok2.setBounds(new Rectangle(270, 50, 70, 25));
ok2.addActionListener(new vel());
add(ok2);
ok2.setVisible(false);
auto = new JButton("Automatico");
auto.setBounds(new Rectangle(50, 80, 100, 25));
auto.addActionListener(new a());
jugar = new JButton("PLAY");
jugar.setBounds(new Rectangle(100, 100, 70, 25));
jugar.addActionListener(new play());
nojugar = new JButton("PAUSE");
nojugar.setBounds(new Rectangle(100, 150, 70, 25));
nojugar.addActionListener(new stop());
setLayout(null);
info.setBounds(new Rectangle(50, 50, 170, 25));
info.setForeground(Color.white);
instruc.setBounds(new Rectangle(970, 50, 570, 25));
instruc.setForeground(Color.white);
instruc2.setBounds(new Rectangle(970, 70, 570, 25));
instruc2.setForeground(Color.white);
add(instruc);
add(instruc2);
add(jugar);
add(nojugar);
jugar.setVisible(false);
nojugar.setVisible(false);
add(info);
info.setVisible(false);
add(ok);
ok.setVisible(false);
add(auto);
numeros.setBounds(new Rectangle(210, 50, 50, 25));
numeros.addItem(1);
numeros.addItem(2);
numeros.addItem(3);
numeros.addItem(4);
numeros.addItem(5);
numeros.addItem(6);
numeros.addItem(7);
numeros.addItem(8);
numeros.addItem(9);
numeros.addItem(10);
add(numeros);
numeros.setVisible(false);
velocidad.setBounds(new Rectangle(150, 50, 100, 25));
velocidad.addItem("Lenta");
velocidad.addItem("Intermedia");
velocidad.addItem("Rapida");
add(velocidad);
velocidad.setVisible(false);
}
public void Mover(int origen, int destino)
{
for (int i = 0; i < 3; i++)
{
System.out.print("stack " + i + ": ");
for (int n : stacks[i])
{
System.out.print(n + ";");
}
System.out.println("");
}
System.out.println("de <" + origen + "> a <" + destino + ">");
stacks[destino].push(stacks[origen].pop());
System.out.println("");
this.validate();
this.repaint();
}
public void hanoi(int origen, int destino, int cuantas)
{
while (parar)
{
}
if (cuantas <= 1)
{
Mover(origen, destino);
}
else
{
hanoi(origen, 3 - (origen + destino), cuantas - 1);
Mover(origen, destino);
hanoi(3 - (origen + destino), destino, cuantas - 1);
}
}
public void paintComponent(Graphics g)
{
ImageIcon fondo = new ImageIcon("fondo.jpg");
g.drawImage(fondo.getImage(), 0, 0, 1366, 768, null);
g.setColor(new Color((int) (Math.random() * 254),
(int) (Math.random() * 255),
(int) (Math.random() * 255)));
g.fillRect(0, 0, 100, 100);
g.setColor(Color.white);
g.fillRect(150, 600, 250, 25);
g.fillRect(550, 600, 250, 25);
g.fillRect(950, 600, 250, 25);
g.setColor(Color.red);
g.fillRect(270, 325, 10, 275);
g.fillRect(270 + 400, 325, 10, 275);
g.fillRect(270 + 800, 325, 10, 275);
int x, y, top = 0;
g.setColor(Color.yellow);
x = 150;
y = 580;
for (int ii : stacks[0])
{
g.fillRect(x + ((ii * 125) / 10), y - (((ii) * 250) / 10), ((10 - ii) * 250) / 10, 20);
}
x = 550;
y = 580;
for (int ii : stacks[1])
{
g.fillRect(x + ((ii * 125) / 10), y - (((ii) * 250) / 10), ((10 - ii) * 250) / 10, 20);
}
x = 950;
y = 580;
for (int ii : stacks[2])
{
g.fillRect(x + ((ii * 125) / 10), y - (((ii) * 250) / 10), ((10 - ii) * 250) / 10, 20);
}
System.out.println("ENTRO");
setOpaque(false);
}
private class play implements ActionListener //manual
{
public void actionPerformed(ActionEvent algo)
{
parar = false;
if (primera = true)
{
hanoi(0, 2, no);
primera = false;
}
}
}
private class stop implements ActionListener //manual
{
public void actionPerformed(ActionEvent algo)
{
parar = true;
}
}
private class vel implements ActionListener //manual
{
public void actionPerformed(ActionEvent algo)
{
if (velocidad.getSelectedItem() == "Lenta")
{
tiempo = 150;
}
else if (velocidad.getSelectedItem() == "Intermedia")
{
tiempo = 75;
}
else
{
tiempo = 50;
}
ok2.setVisible(false);
jugar.setVisible(true);
nojugar.setVisible(true);
}
}
private class a implements ActionListener //auto
{
public void actionPerformed(ActionEvent algo)
{
auto.setVisible(false);
info.setVisible(true);
numeros.setVisible(true);
ok.setVisible(true);
op = 3;
}
}
private class okiz implements ActionListener //ok
{
public void actionPerformed(ActionEvent algo)
{
no = Integer.parseInt(numeros.getSelectedItem().toString());
piezas = no;
if (no > 0 && no < 11)
{
info.setVisible(false);
numeros.setVisible(false);
ok.setVisible(false);
for (int i = no; i > 0; i--)
{
stacks[0].push(i);
}
opcion = 2;
if (op == 3)
{
info.setText("Velocidad: ");
info.setVisible(true);
velocidad.setVisible(true);
ok2.setVisible(true);
}
}
else
{
}
repaint();
}
}
}
the code of the other class that calls the one up is below:
import java.awt.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.border.*;
import java.lang.*;
import java.awt.event.*;
public class aaa extends JPanel
{
private ImageIcon Background;
private JLabel fondo;
public static void main(String[] args) throws IOException
{
JFrame.setDefaultLookAndFeelDecorated(true);
final JPanel cp = new JPanel(new BorderLayout());
JFrame frame = new JFrame ("Torres de Hanoi");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize(550,550);
frame.setVisible(true);
bbb panel = new bbb();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}

Assuming that you see the progress in the println statements but not on the screen, it is because a call to repaint is not synchronous. Swing has a special thread for handling UI - called Event Dispatch Thread. A call to repaint is handled by that thread, but asynchronously - after all the current events scheduled on that thread have been processed.
When you call hanoi in your actionPerformed, that is done on the same UI thread. What happens is that until your recursion is fully done, repaint() calls are just queued. Once the recursion completes (and all stacks have been moved in the model), the UI thread processes all repaint() requests, painting - what i assume - the final state.
What you need to do is to separate the model processing into a separate worker thread. On every recursion step, issue a repaint() call and sleep for a few hundred milliseconds. This will allow the UI thread to repaint the current state of the model and let the user actually trace the progress.

Related

Using a Timer to draw graphics scrolling across JFrame

I'm Making a program that displays a building, some clouds, and trees with Java Graphics. I would like to use a Timer to scroll the clouds across the screen. I'm not sure how to use the timer to continually loop the clouds after it reaches the end of the JFrame
timer is on 126-147
cloud method is on 184 - 239
I have tried to put all the Timer code within the Cloud drawing method but I can't figure out how to use the variable that is tied to the timer to make the clouds move inside the method for the clouds.
Currently, I just have most of the timer stuff outside of the method, and then using 3 different cloud methods, which is redundant.
I'm very new to java so sorry if I have basic mistakes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Elevator extends JFrame implements ActionListener {
private final int DISPLAY_WIDTH = 800;
private final int DISPLAY_HEIGHT = 600;
private JPanel guiPanel, buttonPanel;
private DisplayPanel display;
private JLabel Title;
private JButton Floor;
private JComboBox Select;
int locX, locY;
final int LIMIT = 10;
final int NUM_ROWS = 10;
final int WINDOWWIDTH = 12;
final int WINDOWHEIGHT = 25;
final int WINDOWSPACING = 10;
final int FLOORSPACING = 30;
final int FLOOROFFSET = -10;
final int ELEVATOR_COLUMN = 5;
private static final int NUM_ITERATIONS = 10; //number of floors for combo box selection
private int lvlChoice; //variable holding elevator level choice for item event
private int buildX, buildY, buildW, buildH; //building height dem
Color drkGrn = new Color ( 49, 216, 91); //building ground color
Color flWind = new Color (163, 156, 77); //default floor window color
Color bldCol = new Color (176, 201, 212); // building color
Color crntFl = new Color (255, 247, 0); //current floor color for elevator
Color blu1 = new Color ( 157, 215, 255 ); //cloud colors
Color blu2 = new Color (93, 172, 227);
Color blu3 = new Color ( 62, 167, 240);
Color blu4 = new Color (136, 156, 169);
Color blu5 = new Color (209, 230, 245);
Color plmLeaf = new Color ( 6, 145, 84);
Color trunk =new Color ( 170, 85, 0);
//graphics variables
final int tWidth = 10;
final int tHeiht =120;
public static void main(String[] args) {
Elevator frame = new Elevator();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.initializeVariables();
frame.setUpGUI();
frame.pack();
frame.setVisible(true);
}
public void initializeVariables() {
locX = 300;
locY = 150;
buildX = locX-20;
buildY = locY-10;
buildH =375;
buildW =250;
lvlChoice = 1;
}
public void setUpGUI() {
Container window = getContentPane(); //you attach Jcomponents to this pannel
display = new DisplayPanel();
guiPanel = new JPanel(new FlowLayout());
buttonPanel = new JPanel(new FlowLayout());
//TODO add title panel
/* Title = new JLabel("Elevator");
Title.setFont(new Font(" San Serif", Font.PLAIN, 20));
titlePanel.add(Title);*/
Floor =new JButton("Floor");
Floor.addActionListener(this);
Select = new JComboBox();
for (int i = 0; i < NUM_ITERATIONS; i++) {
Select.addItem(String.valueOf(i + 1)); //this takes the int value and the parses it to a string
}
buttonPanel.add(Select);
buttonPanel.add(Floor);
window.add(buttonPanel, BorderLayout.NORTH);
window.add(guiPanel, BorderLayout.SOUTH);
window.add(display, BorderLayout.CENTER);
}
//cloude1 timer, moviment
Timer tm1 = new Timer(60, this);
int x1 = 800, velX1 = 3; //position of x on cloud and velociity of cloudes
//cloude1 timer, moviment
Timer tm2 = new Timer(50, this);
int x2 = 700, velX2 = 2; //position of x on cloud and velociity of cloudes
//cloude1 timer, moviment
Timer tm3 = new Timer(75, this);
int x3 = 777, velX3 = 2; //position of x on cloud and velociity of cloudes
#Override
public void actionPerformed(ActionEvent e) {
lvlChoice = Integer.parseInt((String) Select.getSelectedItem());
display.repaint();
//make only one of these simplfiy the "x1" to x
x1 = x1 - velX1; //every 2 milliseconds and 2 to the position of x whitch starts at 0
x2 = x2 - velX2; //every 2 milliseconds and 2 to the position of x whitch starts at 0
x3 = x3 - velX3; //every 2 milliseconds and 2 to the position of x whitch starts at 0
}
class DisplayPanel extends JPanel {
DisplayPanel() {
setPreferredSize(new Dimension(DISPLAY_WIDTH, DISPLAY_HEIGHT));
this.setBackground(Color.WHITE);
}
//executes all paint methods
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
backDrop(g2d);
cloud1(g2d, 100, 45);
cloud3(g2d, 50, 30);
buildingLoop(g2d);
cloud2(g2d, 240, 40);
tree(g2d, 140, 400);
tree(g2d, 500, 400);
tree(g2d, 650, 470);
tree(g2d, 600, 420);
tree(g2d, 190, 390);
tree(g2d, 45, 425);
tree(g2d, 75, 450);
}
//static background objects (ground, trees, ect..)
public void backDrop(Graphics2D g2d) {
g2d.setColor(drkGrn);
g2d.fillRect(0,500,getWidth(), getHeight());
}
//new method custom for cloudes
public void cloud1(Graphics2D g2d, int y, int CLDSIZE) {
//cloud 1
//y starts at 100 to be put in method argument
g2d.setColor(blu4);
g2d.fillOval(x1, y, CLDSIZE, CLDSIZE);
g2d.setColor(blu1);
g2d.fillOval(x1 + 15, y-10, CLDSIZE, CLDSIZE);
g2d.setColor(blu3);
g2d.fillOval(x1 + 30, y+10, CLDSIZE, CLDSIZE);
g2d.setColor(blu2);
g2d.fillOval(x1 + 45, y-10, CLDSIZE, CLDSIZE);
g2d.setColor(blu5);
g2d.fillOval(x1 + 57, y, CLDSIZE, CLDSIZE);
tm1.start(); //start the timer
}
public void cloud2(Graphics2D g2d, int y, int CLDSIZE) {
//cloud 2
g2d.setColor(blu5);
g2d.fillOval(x2, y, CLDSIZE,CLDSIZE);
g2d.setColor(blu4);
g2d.fillOval(x2+15, y-20, CLDSIZE,CLDSIZE);
g2d.setColor(blu2);
g2d.fillOval(x2+30, y+10, CLDSIZE,CLDSIZE);
g2d.setColor(blu3);
g2d.fillOval(x2+45, y-20, CLDSIZE,CLDSIZE);
g2d.setColor(blu1);
g2d.fillOval(x2+57, y, CLDSIZE,CLDSIZE);
tm2.start(); //start the timer
}
public void cloud3(Graphics2D g2d, int y, int CLDSIZE) {
//cloud 3
//y starts at 30
g2d.setColor(blu3);
g2d.fillOval(x3, y, CLDSIZE,CLDSIZE);
g2d.setColor(blu2);
g2d.fillOval(x3+15, y-10, CLDSIZE,CLDSIZE);
g2d.setColor(blu4);
g2d.fillOval(x3+30, y+10, CLDSIZE,CLDSIZE);
g2d.setColor(blu5);
g2d.fillOval(x3+45, y-10, CLDSIZE,CLDSIZE);
g2d.setColor(blu1);
g2d.fillOval(x3+57, y, CLDSIZE,CLDSIZE);
tm3.start(); //start the timer
}
public void tree(Graphics2D g2d, int xPoint, int yPoint){
int leafSize =25;
g2d.setColor(trunk);
g2d.fillRect(xPoint+25,yPoint,tWidth, tHeiht);
g2d.setColor(plmLeaf);
g2d.fillOval(xPoint,yPoint, leafSize, leafSize);
g2d.fillOval(xPoint+5,yPoint-10, leafSize, leafSize);
g2d.fillOval(xPoint+5,yPoint+10, leafSize, leafSize);
g2d.fillOval(xPoint+10,yPoint, leafSize, leafSize);
g2d.fillOval(xPoint+10,yPoint+10, leafSize, leafSize);
g2d.fillOval(xPoint+30,yPoint-10, leafSize, leafSize);
g2d.fillOval(xPoint+30,yPoint+10, leafSize, leafSize);
g2d.fillOval(xPoint+35,yPoint, leafSize, leafSize);
g2d.fillOval(xPoint+35,yPoint+10, leafSize, leafSize);
}
//building the building and windows for the building
public void buildingLoop(Graphics2D g2d) {
g2d.setColor(bldCol);
g2d.fillRect(buildX, buildY, buildW, buildH);
g2d.setColor(flWind);
for (int j = 1; j <=NUM_ROWS; j++) { //draws row
for (int i = 0; i <= LIMIT; i++) { //draws window's
if (i == ELEVATOR_COLUMN && j == NUM_ROWS - lvlChoice + 1) {
g2d.setColor(crntFl);
}
g2d.fillRect (i* (WINDOWWIDTH + WINDOWSPACING) + buildX + WINDOWSPACING, buildY + j * FLOORSPACING - FLOOROFFSET, WINDOWWIDTH , WINDOWHEIGHT);
g2d.setColor(flWind);
}
}
}
}
}
I have tried to put all the Timer code within the Cloud drawing method but I can't figure out how to use the variable that is tied to the timer and get that value passed from the actual parameter that is entered from the method.
Currently, I just have most of the timer stuff outside of the method, and then using 3 different cloud methods, which is redundant.
Here is how I'd write it:
If you have any questions you're welcome to ask :)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Elevator extends JFrame implements ActionListener
{
private final int DISPLAY_WIDTH = 800;
private final int DISPLAY_HEIGHT = 600;
private JPanel guiPanel, buttonPanel;
private DisplayPanel display;
private JLabel Title;
private JButton Floor;
private JComboBox Select;
int locX, locY;
final int LIMIT = 10;
final int NUM_ROWS = 10;
final int WINDOWWIDTH = 12;
final int WINDOWHEIGHT = 25;
final int WINDOWSPACING = 10;
final int FLOORSPACING = 30;
final int FLOOROFFSET = -10;
final int ELEVATOR_COLUMN = 5;
private static final int NUM_ITERATIONS = 10; // number of floors for combo box selection
private int lvlChoice; // variable holding elevator level choice for item event
private int buildX, buildY, buildW, buildH; // building height dem
Color drkGrn = new Color(49, 216, 91); // building ground color
Color flWind = new Color(163, 156, 77); // default floor window color
Color bldCol = new Color(176, 201, 212); // building color
Color crntFl = new Color(255, 247, 0); // current floor color for elevator
// an array with all your clouds
private Cloud[] clouds;
private Tree[] trees;
public static void main(String[] args)
{
Elevator frame = new Elevator();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
// added the cloud initialization in there
frame.initializeVariables();
frame.setUpGUI();
frame.pack();
frame.setVisible(true);
// starts ticking each 30 ms
frame.tick();
}
private void tick()
{
// while loop for ever and ever
while (true)
{
// ticks all the clouds
for (int i = 0; i < clouds.length; i++)
{
clouds[i].tick();
}
// updates the JFrame graphics
display.repaint();
// pauses the ticking for 30 ms
try
{
Thread.sleep(30);
} catch (InterruptedException e)
{
}
}
}
public void initializeVariables()
{
locX = 300;
locY = 150;
buildX = locX - 20;
buildY = locY - 10;
buildH = 375;
buildW = 250;
lvlChoice = 1;
// puts 3 clouds in our array
clouds = new Cloud[] { new Cloud(800, 100, 45, 3, true), new Cloud(700, 50, 30, 2, false),
new Cloud(777, 240, 40, 2, false), };
trees = new Tree[] { new Tree(140, 400), new Tree(500, 400), new Tree(650, 470), new Tree(600, 420),
new Tree(190, 390), new Tree(45, 425), new Tree(75, 450) };
}
public void setUpGUI()
{
Container window = getContentPane(); // you attach Jcomponents to this pannel
display = new DisplayPanel();
guiPanel = new JPanel(new FlowLayout());
buttonPanel = new JPanel(new FlowLayout());
// TODO add title panel
/* Title = new JLabel("Elevator");
Title.setFont(new Font(" San Serif", Font.PLAIN, 20));
titlePanel.add(Title);*/
Floor = new JButton("Floor");
Floor.addActionListener(this);
Select = new JComboBox<>();
for (int i = 0; i < NUM_ITERATIONS; i++)
{
Select.addItem(String.valueOf(i + 1)); // this takes the int value and the parses it to a string
}
buttonPanel.add(Select);
buttonPanel.add(Floor);
window.add(buttonPanel, BorderLayout.NORTH);
window.add(guiPanel, BorderLayout.SOUTH);
window.add(display, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent e)
{
lvlChoice = Integer.parseInt((String) Select.getSelectedItem());
display.repaint();
// moves each cloud by the respective velocity
for (int i = 0; i < clouds.length; i++)
clouds[i].x -= clouds[i].velX;
}
class DisplayPanel extends JPanel
{
private static final long serialVersionUID = 1L;
DisplayPanel()
{
setPreferredSize(new Dimension(DISPLAY_WIDTH, DISPLAY_HEIGHT));
this.setBackground(Color.WHITE);
}
// executes all paint methods
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
backDrop(g2d);
for (Cloud c : clouds)
{
if (!c.Foreground) c.draw(g2d);
}
buildingLoop(g2d);
for (Cloud c : clouds)
{
if (c.Foreground) c.draw(g2d);
}
for (Tree t : trees)
{
t.draw(g2d);
}
}
// static background objects (ground, trees, ect..)
public void backDrop(Graphics2D g2d)
{
g2d.setColor(drkGrn);
g2d.fillRect(0, 500, getWidth(), getHeight());
}
// building the building and windows for the building
public void buildingLoop(Graphics2D g2d)
{
g2d.setColor(bldCol);
g2d.fillRect(buildX, buildY, buildW, buildH);
g2d.setColor(flWind);
for (int j = 1; j <= NUM_ROWS; j++)
{ // draws row
for (int i = 0; i <= LIMIT; i++)
{ // draws window's
if (i == ELEVATOR_COLUMN && j == NUM_ROWS - lvlChoice + 1)
{
g2d.setColor(crntFl);
}
g2d.fillRect(i * (WINDOWWIDTH + WINDOWSPACING) + buildX + WINDOWSPACING,
buildY + j * FLOORSPACING - FLOOROFFSET, WINDOWWIDTH, WINDOWHEIGHT);
g2d.setColor(flWind);
}
}
}
}
}
class Cloud
{
int x = 800, y, velX = 3; // position of x on cloud and velociity of cloudes
final Color blu1 = new Color(157, 215, 255); // cloud colors
final Color blu2 = new Color(93, 172, 227);
final Color blu3 = new Color(62, 167, 240);
final Color blu4 = new Color(136, 156, 169);
final Color blu5 = new Color(209, 230, 245);
int Size;
boolean Foreground;
public Cloud(int x, int y, int Size, int velX, boolean Foreground)
{
this.x = x;
this.y = y;
this.Size = Size;
this.velX = velX;
this.Foreground = Foreground;
}
public void tick()
{
x -= velX;
if (x < -100) x = 1000;
}
public void draw(Graphics2D g2d)
{
// cloud 1
// y starts at 100 to be put in method argument
g2d.setColor(blu4);
g2d.fillOval(x, y, Size, Size);
g2d.setColor(blu1);
g2d.fillOval(x + 15, y - 10, Size, Size);
g2d.setColor(blu3);
g2d.fillOval(x + 30, y + 10, Size, Size);
g2d.setColor(blu2);
g2d.fillOval(x + 45, y - 10, Size, Size);
g2d.setColor(blu5);
g2d.fillOval(x + 57, y, Size, Size);
}
}
class Tree
{
int xPoint, yPoint;
public Tree(int xPoint, int yPoint)
{
this.xPoint = xPoint;
this.yPoint = yPoint;
}
public void draw(Graphics2D g2d)
{
int leafSize = 25;
// graphics variables
final int tWidth = 10;
final int tHeiht = 120;
final Color plmLeaf = new Color(6, 145, 84);
final Color trunk = new Color(170, 85, 0);
g2d.setColor(trunk);
g2d.fillRect(xPoint + 25, yPoint, tWidth, tHeiht);
g2d.setColor(plmLeaf);
g2d.fillOval(xPoint, yPoint, leafSize, leafSize);
g2d.fillOval(xPoint + 5, yPoint - 10, leafSize, leafSize);
g2d.fillOval(xPoint + 5, yPoint + 10, leafSize, leafSize);
g2d.fillOval(xPoint + 10, yPoint, leafSize, leafSize);
g2d.fillOval(xPoint + 10, yPoint + 10, leafSize, leafSize);
g2d.fillOval(xPoint + 30, yPoint - 10, leafSize, leafSize);
g2d.fillOval(xPoint + 30, yPoint + 10, leafSize, leafSize);
g2d.fillOval(xPoint + 35, yPoint, leafSize, leafSize);
g2d.fillOval(xPoint + 35, yPoint + 10, leafSize, leafSize);
}
}

How does a class call methods after instantiating another class?

I am very new to this community. I have tried to get up to speed on all rules before posting this question (as well as researching solutions). I apologize if I offended or broke any rules through ignorance. Please also excuse my awful code, I am still learning. Thank you for understanding!
EDIT: I have added additional information and tried different approaches to this issue I'm having. I have reworked part of the code below.
I am building a simple football game with a game field panel and control panel. The game field displays all of the player and tackles on the GUI. The control panel sets the difficulty of the game, starts the timer, and the type of quarterback. I ran into a road block where I have all of my code to compile correctly, but when calling set methods on the GameField class to update the score, it updates the variable but not the actual score through my JTextArea Score keeper.
I have instantiated the ControlPanel within the GameField class. I've also tested with a System.out.println() and it shows that it is indeed updating the variable. Is updating JTextArea allowed between classes?
GameField.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GameField extends JPanel implements KeyListener {
ControlPanel cp = new ControlPanel();
// Game pieces
private JButton playerIcon = new JButton("RB");
private JButton tackleIcon1 = new JButton("LB");
private JButton tackleIcon2 = new JButton("LB");
private JButton fieldGoal = new JButton("FG");
// Player and Tackle locations
private int playerPositionX = 100;
private int playerPositionY = 500;
private int tackle1PositionX = 1200;
private int tackle1PositionY = 400;
private int tackle2PositionX = 1200;
private int tackle2PositionY = 600;
// Player variable speeds
private int playerSpeed = 20;
public GameField() {
setLayout(null);
setBackground(Color.green);
add(playerIcon);
playerIcon.setBounds(new Rectangle(getPlayerPositionX(), getPlayerPositionY(), 80, 30));
add(tackleIcon1);
tackleIcon1.setBounds(new Rectangle(getTackle1PositionX(), getTackle1PositionY(), 100, 50));
add(tackleIcon2);
tackleIcon2.setBounds(new Rectangle(getTackle2PositionX(), getTackle2PositionY(), 100, 50));
add(fieldGoal);
fieldGoal.setBounds(new Rectangle(1600, 100, 100, 800));
playerIsTackled();
setFocusable(true);
addKeyListener(this);
}
public void playerIsTackled() {
Rectangle playerRect = playerIcon.getBounds();
Rectangle tackle1Rect = tackleIcon1.getBounds();
Rectangle tackle2Rect = tackleIcon2.getBounds();
if (playerRect.intersects(tackle1Rect) || playerRect.intersects(tackle2Rect)) {
setPlayerPositionX(100);
setPlayerPositionY(500);
setTackle1PositionX(1200);
setTackle1PositionY(400);
setTackle2PositionX(1200);
setTackle2PositionY(600);
playerIcon.setBounds(getPlayerPositionX(), getPlayerPositionY(), 80, 30);
tackleIcon1.setBounds(getTackle1PositionX(), getTackle1PositionY(), 100, 50);
tackleIcon2.setBounds(getTackle2PositionX(), getTackle2PositionY(), 100, 50);
cp.setCurrentTackles(cp.getCurrentTackles() + 1);
System.out.println(cp.getCurrentTackles());
}
}
public void playerScored() {
Rectangle playerRect = playerIcon.getBounds();
Rectangle fieldGoalRect = fieldGoal.getBounds();
if (playerRect.intersects(fieldGoalRect)) {
setPlayerPositionX(100);
setPlayerPositionY(500);
setTackle1PositionX(1200);
setTackle1PositionY(400);
setTackle2PositionX(1200);
setTackle2PositionY(600);
playerIcon.setBounds(getPlayerPositionX(), getPlayerPositionY(), 80, 30);
tackleIcon1.setBounds(getTackle1PositionX(), getTackle1PositionY(), 100, 50);
tackleIcon2.setBounds(getTackle2PositionX(), getTackle2PositionY(), 100, 50);
cp.setCurrentScore(cp.getCurrentScore() + 1);
System.out.println(cp.getCurrentScore());
}
}
public void moveToPlayer() {
if (getTackle1PositionX() > getPlayerPositionX()) {
setTackle1PositionX(getTackle1PositionX() - 1);
} else {
setTackle1PositionX(getTackle1PositionX() + 1);
}
if (getTackle1PositionY() > getPlayerPositionY()) {
setTackle1PositionY(getTackle1PositionY() - 1);
} else {
setTackle1PositionY(getTackle1PositionY() + 1);
}
getTackleIcon1().setBounds(getTackle1PositionX(), getTackle1PositionY(), 100, 50);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
requestFocusInWindow();
playerIsTackled();
playerScored();
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if (k == e.VK_LEFT && getPlayerPositionX() > 0) {
setPlayerPositionX(getPlayerPositionX() - getPlayerSpeed());
}
if (k == e.VK_RIGHT && getPlayerPositionX() < 1703) {
setPlayerPositionX(getPlayerPositionX() + getPlayerSpeed());
}
if (k == e.VK_UP && getPlayerPositionY() > 0) {
setPlayerPositionY(getPlayerPositionY() - getPlayerSpeed());
}
if (k == e.VK_DOWN && getPlayerPositionY() < 1089) {
setPlayerPositionY(getPlayerPositionY() + getPlayerSpeed());
}
getPlayerIcon().setBounds(getPlayerPositionX(), getPlayerPositionY(), 80, 30);
}
Below is the ControlPanel.java. In the actionPerformed method, you can see I added a getScore().setText() method for updating the score. It correctly updates the score there if I replace the getCurrentScore() method with any integer.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Hashtable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ControlPanel extends JPanel implements ActionListener, ChangeListener {
private JButton start;
private JButton stop;
private JSlider speed;
private JComboBox playerList;
private Timer tim;
private int delay = 1000;
private int i = 0;
private int currentScore = 0;
private int currentTackles = 0;
private JTextArea timer = new JTextArea("Timer: " + 0, 1, 6);
private JTextArea score = new JTextArea("Field Goals: " + currentScore + " Tackles: " + currentTackles, 1, 16);
private String[] playerStyle = {"Slow Runner", "Running Back", "All Star"};
public ControlPanel() {
super();
setBackground(Color.darkGray);
// Game controls
start = new JButton("Start");
stop = new JButton("Stop");
speed = new JSlider(JSlider.HORIZONTAL, 0, 2, 1);
playerList = new JComboBox(getPlayerStyle());
// Slider label
Hashtable labelTable = new Hashtable();
labelTable.put(new Integer(0), new JLabel("Slow"));
labelTable.put(new Integer(1), new JLabel("Normal"));
labelTable.put(new Integer(2), new JLabel("Fast"));
speed.setLabelTable(labelTable);
speed.setPaintLabels(true);
// Combo box dropdown
playerList.setSelectedIndex(1);
// Timer
tim = new Timer(getDelay(), this);
// Add methods
add(start);
add(stop);
add(timer);
add(speed);
add(score);
add(playerList);
// Event listeners
start.addActionListener(this);
stop.addActionListener(this);
speed.addChangeListener(this);
playerList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
String playerChoice = (String) cb.getSelectedItem();
playerList.setSelectedItem(playerChoice);
}
});
// Set focus to false on all game controls
start.setFocusable(false);
stop.setFocusable(false);
speed.setFocusable(false);
playerList.setFocusable(false);
}
#Override
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if (obj == getTim()) {
setI(getI() + 1);
getTimer().setText("Timer: " + getI());
getScore().setText("Field Goals: " + getCurrentScore() + " Tackles: " + getCurrentTackles());
}
if (obj == getStop()) {
getTim().stop();
}
if (obj == getStart()) {
getTim().start();
}
}
#Override
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
int currentSpeed = (int) source.getValue();
if (currentSpeed == 0) {
int delaySpeed = getTim().getDelay();
delaySpeed = (int) 2000;
getTim().setDelay(delaySpeed);
}
if (currentSpeed == 1) {
int delaySpeed = getTim().getDelay();
delaySpeed = (int) 1000;
getTim().setDelay(delaySpeed);
}
if (currentSpeed == 2) {
int delaySpeed = getTim().getDelay();
delaySpeed = (int) 500;
getTim().setDelay(delaySpeed);
}
}
MyJPanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyJPanel extends JPanel {
public MyJPanel() {
super();
setBackground(Color.gray);
setLayout(new BorderLayout());
ControlPanel gm = new ControlPanel();
GameField gf = new GameField();
add(gm, "North");
add(gf, "Center");
}
}
Thanks to #D.B. I have gotten my code to run as intended. Because I only intend to have two JPanels (Control Panel and Game Panel), I added the panels to pass as arguments to each other. After this, the code started working as intended. Thank you for pushing me into the right step! It was such a simple mistake. Special thanks to #DaveyDaveDave for motivating me to push harder!
Changes I made to my main JPanel object:
public class MyJPanel extends JPanel {
ControlPanel gm = new ControlPanel();
GameField gf = new GameField();
public MyJPanel() {
super();
setBackground(Color.gray);
setLayout(new BorderLayout());
add(gm, "North");
add(gf, "Center");
gf.setCp(gm);
gm.setGf(gf);
}
}

I want to put the game on a JPane instead of it being on a JFrame so that I can access it though the use of the GUI however it wont work

Cannot put my game into the GUI without there being issues. When starting the GUI, it shows the game. It glitches out and only fixes after pressing on of the buttons. However the buttons are hidden unless you put your mouse over it. Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class BBSays extends JFrame implements ActionListener, MouseListener
{
private BufferedImage image;
public static BBSays bbsays;
public Renderer renderer;
public static final int WIDTH = 800, HEIGHT = 800;
public int flashed = 0, glowTime, dark, ticks, indexPattern;
public boolean creatingPattern = true;
public ArrayList<Integer> pattern;
public Random random;
private boolean gameOver;
private JPanel game;
JFrame frame = new JFrame("BB8 Says");
private JPanel menu;
private JPanel credits;
ImageIcon bbegif = new ImageIcon("tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif");
public BBSays()
{
Timer timer = new Timer(20, this);
renderer = new Renderer();
frame.setSize(WIDTH +7, HEIGHT +30);
frame.setVisible(true);
frame.addMouseListener(this);
frame.add(renderer);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
start();
timer.start();
menu = new JPanel();
credits = new JPanel();
game = new JPanel();
menu.setBackground(Color.yellow);
credits.setBackground(Color.yellow);
game.setBackground(Color.yellow);
JButton button = new JButton("Start");
JButton button2 = new JButton("Exit");
JButton button4 = new JButton("Start");
JLabel greet = new JLabel(" Welcome to BB8 Says");
JLabel jif = new JLabel(bbegif);
JLabel jif2 = new JLabel(bbegif);
JLabel saus = new JLabel("BB8 Image: https://49.media.tumblr.com/7ba3be87bff2efc009e9cfa889d46b4e/tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setContentPane(game);
frame.invalidate();
frame.validate();
};
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
};
});
menu.setLayout(new GridLayout(2,2));
menu.add(jif2);
menu.add(greet);
menu.add(jif);
menu.add(button);
menu.add(button6);
menu.add(button2);
frame.setVisible(true);
}
private class MenuAction implements ActionListener {
private JPanel panel;
private MenuAction(JPanel pnl) {
this.panel = pnl;
}
#Override
public void actionPerformed(ActionEvent e) {
changePanel(panel);
}
}
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().doLayout();
update(getGraphics());
}
public void start()
{
random = new Random();
pattern = new ArrayList<Integer>();
indexPattern = 0;
dark = 2;
flashed = 0;
ticks = 0;
}
public static void main(String[] args)
{
bbsays = new BBSays();
}
#Override
public void actionPerformed(ActionEvent e)
{
ticks++;
if (ticks % 20 == 0)
{
flashed = 0;
if (dark >= 0)
{
dark--;
}
}
if (creatingPattern)
{
if (dark <= 0)
{
if (indexPattern >= pattern.size())
{
flashed = random.nextInt(40) % 4 + 1;
pattern.add(flashed);
indexPattern = 0;
creatingPattern = false;
}
else
{
flashed = pattern.get(indexPattern);
indexPattern++;
}
dark = 2;
}
}
else if (indexPattern == pattern.size())
{
creatingPattern = true;
indexPattern = 0;
dark = 2;
}
renderer.repaint();
}
public void paint(Graphics2D g)
{
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.yellow);
g.fillRect(0, 0, WIDTH, HEIGHT);
if (flashed == 1)
{
g.setColor(Color.blue);
}
else
{
g.setColor(Color.blue.darker());
}
g.fillRect(0, 0, WIDTH/2, HEIGHT/2);
if (flashed == 2)
{
g.setColor(Color.green);
}
else
{
g.setColor(Color.green.darker());
}
g.fillRect(WIDTH/2, 0, WIDTH/2, HEIGHT/2);
if (flashed == 3)
{
g.setColor(Color.orange);
}
else
{
g.setColor(Color.orange.darker());
}
g.fillRect(0, HEIGHT/2, WIDTH/2, HEIGHT/2);
if (flashed == 4)
{
g.setColor(Color.gray);
}
else
{
g.setColor(Color.gray.darker());
}
g.fillRect(WIDTH/2, HEIGHT/2, WIDTH/2, HEIGHT/2);
g.setColor(Color.BLACK);
g.fillRoundRect(220, 220, 350, 350, 300, 300);
g.fillRect(WIDTH/2 - WIDTH/14, 0, WIDTH/7, HEIGHT);
g.fillRect(0, WIDTH/2 - WIDTH/12, WIDTH, HEIGHT/7);
g.setColor(Color.yellow);
g.setStroke(new BasicStroke(200));
g.drawOval(-100, -100, WIDTH+200, HEIGHT+200);
g.setColor(Color.black);
g.setStroke(new BasicStroke(5));
g.drawOval(0, 0, WIDTH, HEIGHT);
if (gameOver)
{
g.setColor(Color.WHITE);
g.setFont(new Font("Comic Sans", 1, 80));
g.drawString("You let", WIDTH / 2 - 140, HEIGHT / 2 - 70);
g.drawString("down BB8 :(", WIDTH / 2 - 220, HEIGHT / 2 );
g.drawString("Try again!", WIDTH / 2 - 195, HEIGHT / 2 + 80);
}
else
{
g.setColor(Color.WHITE);
g.setFont(new Font("Ariel", 1, 144));
g.drawString(indexPattern + "/" + pattern.size(), WIDTH / 2 - 100, HEIGHT / 2 + 42);
}
}
#Override
public void mousePressed(MouseEvent e)
{
int x = e.getX(), y = e.getY();
if (!creatingPattern && !gameOver)
{
if (x>0 && x<WIDTH/2 && y>0 && y<HEIGHT/2)
{
flashed = 1;
ticks = 1;
}
else if (x>WIDTH/2 && x<WIDTH && y>0 && y<HEIGHT/2)
{
flashed = 2;
ticks = 1;
}
else if (x>0 && x<WIDTH/2 && y>HEIGHT/2 && y<HEIGHT)
{
flashed = 3;
ticks = 1;
}
else if (x>WIDTH/2 && x<WIDTH && y>HEIGHT/2 && y<HEIGHT)
{
flashed = 4;
ticks = 1;
}
if (flashed != 0)
{
if (pattern.get(indexPattern)==flashed)
{
indexPattern++;
}
else
{
gameOver = true;
}
}
else
{
start();
gameOver = true;
}
}
else if (gameOver)
{
start();
gameOver = false;
}
}
Here is the code for the renderer class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Renderer extends JPanel
{
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (BBSays.bbsays != null)
{
BBSays.bbsays.paint((Graphics2D) g);
}
}
}
I think that the issue is here as I use the super. method and want to put the graphics on the game JPanel in the main code. I have tried many ways of doing this but I am not able to put the game on a JPanel.
If you can help it is greatly appreciated.

Trying to solve a proportionnality issue here

In my code i generate randoms integer between 0 and 60 and i draw lines based on these.
I just want my lines fit the ordinate vertical line without touching my randoms integer... I guess it's kind of a mathematics problem but i'm really stuck here!
Here's my code first:
Windows.java:
public class Window extends JFrame{
Panel pan = new Panel();
JPanel container, north,south, west;
public JButton ip,print,cancel,start,ok;
JTextArea timeStep;
JLabel legend;
double time=0;
double temperature=0.0;
Timer chrono;
public static void main(String[] args) {
new Window();
}
public Window()
{
System.out.println("je suis là");
this.setSize(1000,400);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setTitle("Assignment2 - CPU temperature");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container = new JPanel(new BorderLayout());
north = new JPanel();
north.setLayout(new BorderLayout());
ip = new JButton ("New");
north.add(ip, BorderLayout.WEST);
print = new JButton ("Print");
north.add(print,BorderLayout.EAST);
JPanel centerPanel = new JPanel();
centerPanel.add(new JLabel("Time Step (in s): "));
timeStep = new JTextArea("0.1",1,5);
centerPanel.add(timeStep);
start = new JButton("OK");
ListenForButton lForButton = new ListenForButton();
start.addActionListener(lForButton);
ip.addActionListener(lForButton);
print.addActionListener(lForButton);
centerPanel.add(start);
north.add(centerPanel, BorderLayout.CENTER);
west = new JPanel();
JLabel temp = new JLabel("°C");
west.add(temp);
container.add(north, BorderLayout.NORTH);
container.add(west,BorderLayout.WEST);
container.add(pan, BorderLayout.CENTER);
this.setContentPane(container);
this.setVisible(true);
}
private class ListenForButton implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==start)
{
time=Double.parseDouble(timeStep.getText());
System.out.println(time);
chrono = new Timer((int)(1000*time),pan);
chrono.start();
}
if(e.getSource()==ip)
{
JPanel options = new JPanel();
JLabel address = new JLabel("IP Address:");
JTextField address_t = new JTextField(15);
JLabel port = new JLabel("Port:");
JTextField port_t = new JTextField(5);
options.add(address);
options.add(address_t);
options.add(port);
options.add(port_t);
int result = JOptionPane.showConfirmDialog(null, options, "Please Enter an IP Address and the port wanted", JOptionPane.OK_CANCEL_OPTION);
if(result==JOptionPane.OK_OPTION)
{
System.out.println(address_t.getText());
System.out.println(port_t.getText());
}
}
if(e.getSource()==print)
{
chrono.stop();
}
}
}
}
Panel.java:
public class Panel extends JPanel implements ActionListener {
int rand;
int lastrand=0;
ArrayList<Integer> randL = new ArrayList<>();
ArrayList<Integer> tL = new ArrayList<>();
int lastT = 0;
Color red = new Color(255,0,0);
Color green = new Color(0,200,0);
Color blue = new Color (0,0,200);
Color yellow = new Color (200,200,0);
int max=0;
int min=0;
int i,k,inc = 0,j;
int total,degr,moyenne;
public Panel()
{
super();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.8f));
g2.drawLine(20, 20, 20, this.getHeight()-50);
g2.drawLine(20, this.getHeight()-50, this.getWidth()-50, this.getHeight()-50);
g2.drawLine(20, 20, 15, 35);
g2.drawLine(20, 20, 25, 35);
g2.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-45);
g2.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-55);
g.drawString("10", 0, this.getHeight()-85);
g.drawString("20", 0, this.getHeight()-125);
g.drawString("30", 0, this.getHeight()-165);
g.drawString("40", 0, this.getHeight()-205);
g.drawString("50", 0, this.getHeight()-245);
g2.drawString("Maximum: ", 20, this.getHeight()-20);
g2.drawString(Integer.toString(max), 80, this.getHeight()-20);
g2.drawString("Minimum: ", 140, this.getHeight()-20);
g2.drawString(Integer.toString(min), 200, this.getHeight()-20);
g2.drawString("Average: ", 260, this.getHeight()-20);
g2.drawString(Integer.toString(moyenne), 320, this.getHeight()-20);
g2.setColor(red);
g2.drawLine(500, this.getHeight()-25, 540, this.getHeight()-25);
g2.setColor(new Color(0,0,0));
g2.drawString(": Maximum", 560, this.getHeight()-20);
g2.setColor(blue);
g2.drawLine(640, this.getHeight()-25, 680, this.getHeight()-25);
g2.setColor(new Color(0,0,0));
g2.drawString(": Minimum", 700, this.getHeight()-20);
g2.setColor(green);
g2.drawLine(780, this.getHeight()-25, 820, this.getHeight()-25);
g2.setColor(new Color(0,0,0));
g2.drawString(": Average", 840, this.getHeight()-20);
if(!randL.isEmpty()){
g2.setColor(red);
g2.drawLine(15, this.getHeight()-50-max, this.getWidth()-50,this.getHeight()-50-max);
g2.setColor(blue);
g2.drawLine(15, this.getHeight()-50-min, this.getWidth()-50,this.getHeight()-50-min);
g2.setColor(green);
g2.drawLine(15, this.getHeight()-50-moyenne, this.getWidth()-50,this.getHeight()-50-moyenne);
}
for(i = 0; i<tL.size(); i++){
int temp = randL.get(i);
int t = tL.get(i);
g2.setColor(new Color(0,0,0));
g2.drawLine(20+t, this.getHeight()-50-temp, 20+t, this.getHeight()-50);
// Ellipse2D circle = new Ellipse2D.Double();
//circle.setFrameFromCenter(20+t, this.getHeight()-50, 20+t+2, this.getHeight()-52);
}
for(j=0;j<5;j++)
{
inc=inc+40;
g2.setColor(new Color(0,0,0));
g2.drawLine(18, this.getHeight()-50-inc, 22, this.getHeight()-50-inc);
}
inc=0;
}
#Override
public void actionPerformed(ActionEvent e) {
rand = (int)(Math.random() * (60));
lastT += 80;
randL.add(rand);
tL.add(lastT);
Object obj = Collections.max(randL);
max = (int) obj;
Object obj2 = Collections.min(randL);
min = (int) obj2;
if(!randL.isEmpty()) {
degr = randL.get(k);
total += degr;
moyenne=total/randL.size();
}
k++;
if(randL.size()>=12)
{
randL.removeAll(randL);
tL.removeAll(tL);
lastT = 0;
k=0;
degr=0;
total=0;
moyenne=0;
}
repaint();
}
}
And here it what i gives me :
Sorry it's a real mess!
Any thoughts ?
Thanks.
You need to stop working with absolute/magical values, and start using the actual values of the component (width/height).
The basic problem is a simple calculation which takes the current value divides it by the maximum value and multiples it by the available width of the allowable area
int length = (value / max) * width;
value / max generates a percentage value of 0-1, which you then use to calculate the percentage of the available width of the area it will want to use.
The following example places a constraint (or margin) on the available viewable area, meaning all the lines need to be painted within that area and not use the entire viewable area of the component
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DrawLine {
public static void main(String[] args) {
new DrawLine();
}
public DrawLine() {
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 {
public TestPane() {
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int margin = 20;
int width = getWidth() - (margin * 2);
int height = getHeight() - (margin * 2);
int x = margin;
for (int index = 0; index < 4; index++) {
int y = margin + (int)(((index / 3d) * height));
int length = (int)(((index + 1) / 4d) * width);
g2d.drawLine(x, y, x + length, y);
}
g2d.dispose();
}
}
}

Overlapping background Graphics in a Memory Game Applet

Okay I have been working on this memory game applet in java for a while now and I have all the sorting and matching algorithms all figured out, I am just having a wretched time trying to get my GUI to function properly. Whenever I click on one of the "cards" to "flip", I end up with a column of the cards being created while their backs counterparts remain under the cards until you go over it with the cursor. It is all very frustrating as I am not quite sure why half of this is happening or how to stop it. Here is my Display class:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.*;
public class Display extends JPanel implements MouseListener {
private String[] Colors = Card.validColors();
private String[] Shapes = Card.validShapes();
private Component[] place;
private JButton[][] buttonGrid= new JButton[6][6];
private Rectangle[][] triggers = new Rectangle[6][6];
private Board game;
private Polygon star = new Polygon();
private Card pick;
private boolean turnPhase2 = false;
private Font serifNames = new Font(Font.SERIF, Font.PLAIN, 18);
private Font serifCards = new Font(Font.SERIF, Font.ROMAN_BASELINE, 36);
private JPanel panel = new JPanel();
public Display() {
this.setBackground(Color.BLACK);
this.setLayout(new GridBagLayout());
panel.setSize(590, 410);
panel.setLayout(new GridLayout(6, 6, 10, 10));
panel.setOpaque(false);
generateStar();
buildBoard();
fillButtonArray();
this.addMouseListener(this);
this.add(panel);
System.out.println(getFontMetrics(serifCards));
System.out.println(getFontMetrics(serifNames));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
place = panel.getComponents();
for (int i = 0; i < place.length; i++) {
paintBack(g, place[i].getX() + 25, place[i].getY() + 35);
}
displayNames(g);
displayTurn(g);
if (game.flippedCard() != null) {
int[] xy = game.flippedCardLocation();
paintCard(g, game.flippedCard(), xy[0], xy[1]);
}
}
/**
* This method builds the game board.
*
*
*/
private void buildBoard() {
game = new Board(buildDeck());
}
/**
* This method creates a "deck" of cards with which we can create the game board.
*
* #return deck Returns the deck of Card objects in the form of an ArrayList.
*/
private ArrayList<Card> buildDeck() {
ArrayList<Card> deck = new ArrayList<Card>();
for (int i = 0; i < Colors.length; i++) {
for (int s = 0; s < Shapes.length; s++) {
Card first = new Card(Shapes[s], Colors[i]);
Card second = new Card(Shapes[s], Colors[i]);
deck.add(first);
deck.add(second);
}
}
System.out.println(deck.size());
return deck;
}
private void fillButtonArray() {
for (int i = 0; i < 6; i++) {
for (int n = 0; n < 6; n++) {
JButton button = new JButton();
button.setPreferredSize(new Dimension(90, 60));
button.addMouseListener(this);
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
buttonGrid[i][n] = button;
}
}
fillGrid();
}
private void fillGrid() {
panel.setBounds(25, 35, panel.getSize().width, panel.getSize().height);
int count = 0;
for (int i = 0; i < 6; i++) {
for (int s = 0; s < 6; s++) {
panel.add(buttonGrid[i][s]);
place = panel.getComponents();
int x = panel.getComponent(count).getBounds().x;
int y = panel.getComponent(count).getBounds().y;
Rectangle rect = new Rectangle(x, y, 90, 60);
triggers[i][s] = rect;
}
}
}
private void paintBack(Graphics g, int x, int y) {
g.setColor(Color.WHITE);
g.drawRoundRect(x, y, 90, 60, 2, 4);
g.fillRoundRect(x, y, 90, 60, 2, 4);
g.setColor(Color.BLACK);
g.setFont(serifCards);
g.drawString("M", x + 28, y + 42);
}
private void paintCard(Graphics g, Card card, int x, int y) {
g.setColor(Color.GRAY);
g.drawRoundRect(x, y, 90, 60, 2, 4);
g.fillRoundRect(x, y, 90, 60, 2, 4);
String color = card.getColor();
String shape = card.getShape();
if (shape.equals("Star")) {
g.setColor(pickColor(color));
star.translate(x + 25, y + 10);
g.drawPolygon(star);
g.fillPolygon(star);
}
else if (shape.equals("Circle")) {
g.setColor(pickColor(color));
g.drawOval(x + 25, y + 10, 40, 40);
g.fillOval(x + 25, y + 10, 40, 40);
}
else if (shape.equals("Square")) {
g.setColor(pickColor(color));
g.drawRect(x + 25, y + 10, 40, 40);
g.fillRect(x + 25, y + 10, 40, 40);
}
}
private void displayNames(Graphics g) {
g.setFont(serifNames);
int[] scores = game.getCurrentScores();
for (int i = 0; i < scores.length; i++) {
if (i == 0) {
g.setColor(Color.CYAN);
g.drawString("Cyan: " + scores[i], 10, 24);
}
else if (i == 1) {
g.setColor(Color.ORANGE);
g.drawString("Orange: " + scores[i], 560, 24);
}
else if (i == 2) {
g.setColor(Color.MAGENTA);
g.drawString("Magenta: " + scores[i], 10, 470);
}
else {
g.setColor(Color.WHITE);
g.drawString("White: " + scores[i], 569, 470);
}
}
}
private void displayTurn(Graphics g) {
int player = game.getCurrentPlayer();
if (player == 0) {
g.setColor(Color.CYAN);
String str = "Cyan's Turn";
g.drawString(str, 640 / 2 - 48, 24);
//System.out.println(getFontMetrics(serifNames).stringWidth(str) / 2 + " Cyan");
}
else if (player == 1) {
g.setColor(Color.ORANGE);
String str = "Orange's Turn";
g.drawString(str, 640 / 2 - 52, 24);
//System.out.println(getFontMetrics(serifNames).stringWidth(str) / 2 + " Orange");
}
else if (player == 2) {
g.setColor(Color.MAGENTA);
String str = "Magenta's Turn";
g.drawString(str, 640 / 2 - 57, 24);
//System.out.println(getFontMetrics(serifNames).stringWidth(str) / 2 + " Magenta");
}
else {
g.setColor(Color.WHITE);
String str = "White's Turn";
g.drawString(str, 640 / 2 - 47, 24);
//System.out.println(getFontMetrics(serifNames).stringWidth(str) / 2 + " White");
}
}
private void findTrigger(int x, int y) {
for (int i = 0; i < 6; i++) {
if () {
}
for (int s = 0; s < 6; s++) {
Rectangle rectTest = triggers[i][s];
if (x >= rectTest.getMinX() &&
x <= rectTest.getMaxX() &&
y >= rectTest.getMinY() &&
y <= rectTest.getMaxY()) {
Graphics g = getGraphics();
paintCard(g, game.flip(i,s), buttonGrid[i][s].getBounds().x, buttonGrid[i][s].getBounds().y);
break;
}
}
}
}
private void generateStar() {
star.addPoint(20, 0);
star.addPoint(25, 15);
star.addPoint(40, 15);
star.addPoint(28, 24);
star.addPoint(32, 40);
star.addPoint(20, 30);
star.addPoint(8, 40);
star.addPoint(12, 24);
star.addPoint(0, 15);
star.addPoint(15, 15);
}
private Color pickColor(String color) {
if (color.equals("Black")) {
return Color.BLACK;
}
if (color.equals("Yellow")) {
return Color.YELLOW;
}
if (color.equals("Green")) {
return Color.GREEN;
}
if (color.equals("Blue")) {
return Color.BLUE;
}
if (color.equals("Red")) {
return Color.RED;
}
if (color.equals("Purple")) {
return new Color(128, 0, 255);
}
return null;
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked");
int x = e.getX();
System.out.println(x + " is x");
int y = e.getY();
System.out.println(y + " is Y");
System.out.println(panel.getWidth() + 25);
System.out.println(panel.getHeight() + 35);
System.out.println("Finding the trigger rectangle");
findTrigger(x, y);
}
#Override
public void mouseEntered(MouseEvent e) {
//System.out.println("Mouse Entered");
}
#Override
public void mouseExited(MouseEvent e) {
//System.out.println("Mouse Exited");
}
#Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed");
}
#Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released");
}
}
Some side notes is that the actual game is handled by the board object and has all the methods needed to create and run a multiplayer memory game and the Card object only contains two strings of the shape and color of what is to be matched by the game. And finally the last class is the Memory class which I will provide:
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JApplet;
public class Memory extends JApplet {
private Display _theDisplay;
final int width = 640;
final int height = 480;
private Action reDraw = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
repaint();
}
};
public Memory() {
_theDisplay = new Display();
}
public void init() {
setSize(width, height);
setBackground(Color.BLACK);
this.add(_theDisplay);
}
}
Please any tips would be incredibly helpful, thanks in advance!
Your graphics appear messed. Problems I see:
You call getGraphics() on a component to draw with its Graphics context, but please understand that a Graphics object obtained in this way will not persist, and can thus mess up or even cause a NPE to be thrown.
Better to do all-passive Graphics via your paintComponent(...) method. If you need any pre-made drawings, do these in BufferedImages, and draw the BufferedImages in the JComponent's paintComponent(...) method.
Rather than have your Display JPanel do all the painting of the cards and the backs, I suggest that each Card be its own separate object with its own state, that paints itself correctly depending on its state. You may wish to have this extend a JComponent, or have it be a logical entity that is then painted by your Display JPanel, up to you, but separate out the logic from the display to simplify your coding and debugging.
A major problem looks to be in your findTrigger(...) method and that is where you should concentrate your efforts. You should use the mouseClick to change the state of the logical Card (as described above) that is clicked and then call repaint() on the Display JPanel (this) if the Cards are painted in paintComponent(...).
Else if the Cards paint themselves, consider having them be JLabels and simply swap ImageIcons, likely the easiest way to "flip" cards.
Your main problem is that of a program mis-behavior. I have not seen the cause of this on a quick overview of your code, and suggest that you use a debugger or println statements to first and foremost try to isolate the problem.

Categories

Resources