JFrame shows up grey, blank - java

I have a problem where my frame shows up as grey, but then after a few seconds it displays the image as my for loop is finishing. I need to make it so that it displays the image correctly once it opens in order to show the loop. Here is my entire code, sorry about the length:
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JFrame{
public void MainFrame() {
setTitle("Game");
setSize(1300, 650);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBackground(Color.WHITE);
Dice diceObject = new Dice();
add(diceObject);
}
}
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class Dice extends JPanel {
public static int pause(int n)
{
try {
Thread.sleep(n);
} catch(InterruptedException e) {
}
return n;
}
public void Dice() {
setDoubleBuffered(true);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int num = 0;
for (int i = 0; i < 7; i++) {
Random generator= new Random();
int number = generator.nextInt(6)+1;
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.drawRoundRect(550, 150, 200, 200, 50, 50);
System.out.println("Test");
if (number == 1) { //Roll one
num = 1;
g.setColor(new Color (0, 0, 0));
g.fillOval(640, 240, 20, 20);
pause(100);
} if (number == 2) { //Roll two
num = 2;
g.setColor(new Color (0, 0, 0));
g.fillOval(590, 290, 20, 20);
g.fillOval(690, 190, 20, 20);
pause(100);
} if (number == 3) { //Roll three
num = 3;
g.setColor(new Color (0, 0, 0));
g.fillOval(590, 290, 20, 20);
g.fillOval(640, 240, 20, 20);
g.fillOval(690, 190, 20, 20);
pause(100);
} if (number == 4) { //Roll four
num = 4;
g.setColor(new Color (0, 0, 0));
g.fillOval(590, 290, 20, 20);
g.fillOval(590, 190, 20, 20);
g.fillOval(690, 290, 20, 20);
g.fillOval(690, 190, 20, 20);
pause(100);
} if (number == 5) { //Roll five
num = 5;
g.setColor(new Color (0, 0, 0));
g.fillOval(590, 290, 20, 20);
g.fillOval(590, 190, 20, 20);
g.fillOval(640, 240, 20, 20);
g.fillOval(690, 290, 20, 20);
g.fillOval(690, 190, 20, 20);
pause(100);
} if (number == 6) { //Roll six
num = 6;
g.setColor(new Color (0, 0, 0));
g.fillOval(590, 190, 20, 20);
g.fillOval(590, 240, 20, 20);
g.fillOval(590, 290, 20, 20);
g.fillOval(690, 190, 20, 20);
g.fillOval(690, 240, 20, 20);
g.fillOval(690, 290, 20, 20);
pause(100);
}
}
g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
g.drawString("You rolled a " + num, 590, 100);
pause(1000);
}
}
Thanks in advance.

The first problem is the fact that you are setting the frame visible BEFORE you add anything to it...
Instead, try calling setVisible last
public class MainFrame extends JFrame{
public void MainFrame() {
setTitle("Game");
setSize(1300, 650);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
Dice diceObject = new Dice();
add(diceObject);
setVisible(true);
}
}
You should also avoid extending directly from JFrame as you're not adding any new functionality to the class and need to take into consideration Initial Threads and ensure you are starting the main UI within the context of the Event Dispatching Thread.
The rest of your problems relate to your previous question
Updated with working example
Cause I'm lazy, I've utilised the Graphics 2D API to draw the dots. I did this because many of the dots appear in the same locations for many of the numbers...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DiceRoller {
public static void main(String[] args) {
new DiceRoller();
}
public DiceRoller() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Die());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Die extends JPanel {
private int number = 1;
public Die() {
Timer timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
number = (int) (Math.round((Math.random() * 5) + 1));
repaint();
}
});
timer.setRepeats(true);
timer.setInitialDelay(0);
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(220, 220);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth();
int height = getHeight();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.BLACK);
g2d.drawRoundRect(10, 10, width - 20, height - 20, 50, 50);
List<Shape> dots = new ArrayList<>(6);
if (number == 1 || number == 3 || number == 5) {
int x = (width - 20) / 2;
int y = (height - 20) / 2;
dots.add(new Ellipse2D.Float(x, y, 20, 20));
}
if (number == 2 || number == 3 || number == 4 || number == 5 || number == 6) {
int x = ((width / 2) - 20) / 2;
int y = ((height / 2) - 20) / 2;
dots.add(new Ellipse2D.Float(x, y, 20, 20));
dots.add(new Ellipse2D.Float(x + (width / 2), y + (height / 2), 20, 20));
}
if (number == 4 || number == 5 || number == 6) {
int x = (width / 2) + (((width / 2) - 20) / 2);
int y = ((height / 2) - 20) / 2;
dots.add(new Ellipse2D.Float(x, y, 20, 20));
dots.add(new Ellipse2D.Float(x - (width / 2), y + (height / 2), 20, 20));
}
if (number == 6) {
int x = (((width / 2) - 20) / 2);
int y = (height - 20) / 2;
dots.add(new Ellipse2D.Float(x, y, 20, 20));
dots.add(new Ellipse2D.Float(x + (width / 2), y, 20, 20));
}
for (Shape dot : dots) {
g2d.fill(dot);
}
g2d.dispose();
}
}
}

Related

Eclipse game window opening very small

Whenever I run my game, the game window opens up very small, like below, how can I fix this. I believe it is something to do with JFrame.setPreferredSize(new Dimension(x,y)); but I don't know where to implement this in the code for it to work.
This is what it shows:
enter image description here
Where do I implement the code? Like in the class, outside the class, or do I need to show my current code for someone to help me here?
This is my code:
public class RoadAppMain extends JFrame {
private static final int D_W = 1920; //Window dimension
private static final int D_H = 1280; //Window height
int grassWidth = 1920; //Width of grass
int vanishHeight = 768; // Height of vanishing point
int roadWidth = 900; //Width of the road
int rumbleLen = 800; //Length of each track stripe
double camDist = 0.8; //Camera distance
int N; //Size of each row or line
int playerPosX = 0; //Movement of player left and right
int playerPosY = 0; //Movement of player up and down
List<Line> lines = new ArrayList<RoadAppMain.Line>();
List<Integer> listValues = new ArrayList<Integer>();
DrawPanel drawPanel = new DrawPanel();
public RoadAppMain() {
for (int i = 0; i < 1600; i++) {
Line line = new Line();
line.z = i * rumbleLen;
int curveAngle = (int) (Math.random() * 15 + 1);
if (i > 20 && i < 70) {
line.curve = curveAngle;
}
else if (i > 100 && i < 150) {
line.curve = -curveAngle;
}
else if (i > 180 && i < 230) {
line.curve = curveAngle;
}
else if (i > 260 && i < 310) {
line.curve = -curveAngle;
}
else if (i > 340 && i < 390) {
line.curve = curveAngle;
}
else if (i > 400 && i < 420) {
}
lines.add(line);
}
N = lines.size();
//Handles action events by user
ActionListener listener = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
drawPanel.repaint();
}
};
Timer timer = new Timer(1, listener);
timer.start();
add(drawPanel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
//Moves screen using arrow keys
private class DrawPanel extends JPanel {
public DrawPanel() {
String VK_LEFT = "VK_LEFT";
KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW); //necessary
inputMap.put(W, VK_LEFT);
ActionMap actionMap = getActionMap(); //necessary
actionMap.put(VK_LEFT, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
playerPosX -= 200;
drawPanel.repaint();
}
});
String VK_RIGHT = "VK_RIGHT";
KeyStroke WVK_RIGHT = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
inputMap.put(WVK_RIGHT, VK_RIGHT);
actionMap.put(VK_RIGHT, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
playerPosX += 200;
drawPanel.repaint();
}
});
String VK_UP = "VK_UP";
KeyStroke WVK_UP = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
inputMap.put(WVK_UP, VK_UP);
actionMap.put(VK_UP, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
playerPosY += 200;
drawPanel.repaint();
}
});
String VK_DOWN = "VK_DOWN";
KeyStroke WVK_DOWN = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
inputMap.put(WVK_DOWN, VK_DOWN);
actionMap.put(VK_DOWN, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
playerPosY -= 200;
drawPanel.repaint();
}
});
}
//Drawing components feature
protected void paintComponent(Graphics g) {
drawValues(g);
g.setColor(Color.black);
g.fillRect(0, 0, 1920, 395);
}
}
private void drawValues(Graphics g) {
int startPos = playerPosY / rumbleLen;
double x = 0; //Initial X position of screen on road
double dx = 0; //Correlation between the angle of the road and player
double maxY = vanishHeight;
int camH = 1700 + (int) lines.get(startPos).y; //Height of the camera
//Starting position
for (int n = startPos; n < startPos + 300; n++) {
Line l = lines.get(n % N); //Position of line
l.project(playerPosX - (int) x, camH, playerPosY);
x += dx;
dx += l.curve;
if (l.Y > 0 && l.Y < maxY) {
maxY = l.Y;
Color grass = ((n / 2) % 2) == 0 ? new Color(21, 153, 71) : new Color(22, 102, 52); //Color for grass (first is for lighter, second for darker)
Color rumble = ((n / 2) % 2) == 0 ? new Color(255, 255, 255) : new Color(222, 4, 4); // Color for rumble (first is white, second is red)
Color road = new Color(54, 52, 52); // Color of road or asphalt
Color midel = ((n / 2) % 2) == 0 ? new Color(255, 255, 255) : new Color(54, 52, 52); //Color of hashed lines (first for white lines, second for gap)
Color car = new Color(104, 104, 104);
Color tire = new Color(0, 0, 0);
Color stripe = new Color(0, 0, 0);
Color light = new Color(253, 0, 0);
Color hood = new Color(0, 0, 0);
Color frame = new Color(0,0,255);
Line p = null;
if (n == 0) {
p = l;
} else {
p = lines.get((n - 1) % N);
}
draw(g, grass, 0, (int) p.Y, grassWidth, 0, (int) l.Y, grassWidth); //(Graphics g, Color c, int x1, int y1, int w1, int x2, int y2, int w2)
draw(g, rumble, (int) p.X, (int) p.Y, (int) (p.W * 2.03), (int) l.X, (int) l.Y, (int) (l.W * 2.03)); //Affects width of rumble
draw(g, road, (int) p.X, (int) p.Y, (int) (p.W * 1.8), (int) l.X, (int) l.Y, (int) (l.W * 1.8));
draw(g, midel, (int) p.X, (int) p.Y, (int) (p.W * 0.78), (int) l.X, (int) l.Y, (int) (l.W * 0.78)); //ADD HERE
draw(g, road, (int) p.X, (int) p.Y, (int) (p.W * 0.7), (int) l.X, (int) l.Y, (int) (l.W* 0.7)); //To cover the gap in between each midel. Must be after to allow it to colour over it
draw(g, car, 965, 927, 125, 965, 1005, 125);
draw(g, tire, 875, 1005, 35, 875, 1050, 35);
draw(g, tire, 1055, 1005, 35, 1055, 1050, 35);
draw(g, stripe, 1050, 965, 30, 870, 980, 30);
draw(g, stripe, 1050, 950, 30, 870, 965, 30);
draw(g, hood, 965, 880, 90, 965, 927, 125);
draw(g, light, 875, 950, 5, 875, 980, 5);
draw(g, light, 890, 950, 5, 890, 980, 5);
draw(g, light, 905, 950, 5, 905, 980, 5);
draw(g, light, 1025, 950, 5, 1025, 980, 5);
draw(g, light, 1040, 950, 5, 1040, 980, 5);
draw(g, light, 1055, 950, 5, 1055, 980, 5);
draw(g, frame, 965, 874, 86, 965, 880, 90);
draw(g, light, 965, 874, 35, 965, 880, 45);
}
}
}
void draw(Graphics g, Color c, int x1, int y1, int w1, int x2, int y2, int w2) {
Graphics g9d = g;
int[] x9Points = { x1 - w1, x2 - w2, x2 + w2, x1 + w1 };
int[] y9Points = { y1, y2, y2, y1 };
int n9Points = 4;
g9d.setColor(c);
g9d.fillPolygon(x9Points, y9Points, n9Points);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new RoadAppMain();
}
});
}
class Line {
double x, y, z;
double X, Y, W;
double scale, curve;
public Line() {
curve = x = y = z = 0;
}
void project(int camX, int camY, int camZ) {
scale = camDist / (z - camZ);
X = (1 + scale * (x - camX)) * grassWidth / 2;
Y = (1 - scale * (y - camY)) * vanishHeight / 2;
W = scale * roadWidth * grassWidth / 2;
}
}
}

Move game screen diagonally using arrow keys

I'm creating a car game where it creates a 3D pseudo effect by moving the screen backwards and the car itself just stays still on the map, using the arrow keys. It only takes in one input at a time, meaning you have to let go of an arrow key first. I have some code that I've got to show what I'm aiming for the screen to do as well, as the screen only moves forwards, backwards, left, and right.
Example of what I want the screen to do:
public class Main extends JFrame {
private static final long serialVersionUID = 7722803326073073681L;
private boolean left = false;
private boolean up = false;
private boolean down = false;
private boolean right = false;
public JLabel lbl = new JLabel ("mn");
public Main() {
// Just setting up the window and objects
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
lbl.setBounds(100, 100, 20, 20);
add(lbl);
setLocationRelativeTo(null);
// Key listener, will not move the JLabel, just set where to
addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {}
#Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) left = false;
if (e.getKeyCode() == KeyEvent.VK_RIGHT) right = false;
if (e.getKeyCode() == KeyEvent.VK_UP) up = false;
if (e.getKeyCode() == KeyEvent.VK_DOWN) down = false;
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) left = true;
if (e.getKeyCode() == KeyEvent.VK_RIGHT) right = true;
if (e.getKeyCode() == KeyEvent.VK_UP) up = true;
if (e.getKeyCode() == KeyEvent.VK_DOWN) down = true;
}
});
// This thread will read the 4 variables left/right/up/down at every 30 milliseconds
// It will check the combination of keys (left and up, right and down, just left, just up...)
// And move the label 3 pixels
new Thread(new Runnable() {
#Override
public void run() {
try {
while (true) {
if (left && up) {
lbl.setBounds(lbl.getX() - 10, lbl.getY() - 10, 20, 20);
} else if (left && down) {
lbl.setBounds(lbl.getX() - 10, lbl.getY() + 10, 20, 20);
} else if (right && up) {
lbl.setBounds(lbl.getX() + 10, lbl.getY() - 10, 20, 20);
} else if (right && down) {
lbl.setBounds(lbl.getX() + 10, lbl.getY() + 10, 20, 20);
} else if (left) {
lbl.setBounds(lbl.getX() - 10, lbl.getY(), 20, 20);
} else if (up) {
lbl.setBounds(lbl.getX(), lbl.getY() - 10, 20, 20);
} else if (right) {
lbl.setBounds(lbl.getX() + 10, lbl.getY(), 20, 20);
} else if (down) {
lbl.setBounds(lbl.getX(), lbl.getY() + 10, 20, 20);
}
Thread.sleep(30);
}
} catch (Exception ex) {
ex.printStackTrace();
System.exit(0);
}
}
}).start();
}
public static void main(String[] args) {
new Main();
}
}
However, since the code above only moves the letters, I'm having a lot of difficulty in implementing it in the game as well. This is what I'm currently using to move the screen. (If I were to delete this method, nothing would appear).
What is currently happening:
public class RoadAppMain extends JFrame {
private static final int D_W = 1920; //Window dimension
private static final int D_H = 1280; //Window height
int grassWidth = 1920; //Width of grass
int vanishHeight = 768; // Height of vanishing point
int roadWidth = 900; //Width of the road
int rumbleLen = 800; //Length of each track stripe
double camDist = 0.8; //Camera distance
int N; //Size of each row or line
int playerPosX = 0; //Movement of player left and right
int playerPosY = 0; //Movement of player up and down
List<Line> lines = new ArrayList<RoadAppMain.Line>();
List<Integer> listValues = new ArrayList<Integer>();
DrawPanel drawPanel = new DrawPanel();
public RoadAppMain() {
for (int i = 0; i < 1600; i++) {
Line line = new Line();
line.z = i * rumbleLen;
int curveAngle = (int) (Math.random() * 15 + 1);
if (i > 20 && i < 70) {
line.curve = curveAngle;
}
else if (i > 100 && i < 150) {
line.curve = -curveAngle;
}
else if (i > 180 && i < 230) {
line.curve = curveAngle;
}
else if (i > 260 && i < 310) {
line.curve = -curveAngle;
}
else if (i > 340 && i < 390) {
line.curve = curveAngle;
}
else if (i > 400 && i < 420) {
}
lines.add(line);
}
N = lines.size();
//Handles action events by user
ActionListener listener = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
drawPanel.repaint();
}
};
Timer timer = new Timer(1, listener);
timer.start();
add(drawPanel);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
//Moves screen using arrow keys - THIS IS THE PART IM TALKING ABOUT
private class DrawPanel extends JPanel {
public DrawPanel() {
String VK_LEFT = "VK_LEFT";
KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW); //necessary
inputMap.put(W, VK_LEFT);
ActionMap actionMap = getActionMap(); //necessary
actionMap.put(VK_LEFT, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
playerPosX -= 200;
drawPanel.repaint();
}
});
String VK_RIGHT = "VK_RIGHT";
KeyStroke WVK_RIGHT = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
inputMap.put(WVK_RIGHT, VK_RIGHT);
actionMap.put(VK_RIGHT, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
playerPosX += 200;
drawPanel.repaint();
}
});
String VK_UP = "VK_UP";
KeyStroke WVK_UP = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
inputMap.put(WVK_UP, VK_UP);
actionMap.put(VK_UP, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
playerPosY += 200;
drawPanel.repaint();
}
});
String VK_DOWN = "VK_DOWN";
KeyStroke WVK_DOWN = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
inputMap.put(WVK_DOWN, VK_DOWN);
actionMap.put(VK_DOWN, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
playerPosY -= 200;
drawPanel.repaint();
}
});
}
//Drawing components feature
protected void paintComponent(Graphics g) {
drawValues(g);
g.setColor(Color.black);
g.fillRect(0, 0, 1920, 395);
}
}
private void drawValues(Graphics g) {
int startPos = playerPosY / rumbleLen;
double x = 0; //Initial X position of screen on road
double dx = 0; //Correlation between the angle of the road and player
double maxY = vanishHeight;
int camH = 1700 + (int) lines.get(startPos).y; //Height of the camera
//Starting position
for (int n = startPos; n < startPos + 300; n++) {
Line l = lines.get(n % N); //Position of line
l.project(playerPosX - (int) x, camH, playerPosY);
x += dx;
dx += l.curve;
if (l.Y > 0 && l.Y < maxY) {
maxY = l.Y;
Color grass = ((n / 2) % 2) == 0 ? new Color(21, 153, 71) : new Color(22, 102, 52); //Color for grass (first is for lighter, second for darker)
Color rumble = ((n / 2) % 2) == 0 ? new Color(255, 255, 255) : new Color(222, 4, 4); // Color for rumble (first is white, second is red)
Color road = new Color(54, 52, 52); // Color of road or asphalt
Color midel = ((n / 2) % 2) == 0 ? new Color(255, 255, 255) : new Color(54, 52, 52); //Color of hashed lines (first for white lines, second for gap)
Color car = new Color(104, 104, 104);
Color tire = new Color(0, 0, 0);
Color stripe = new Color(0, 0, 0);
Color light = new Color(253, 0, 0);
Color hood = new Color(0, 0, 0);
Color frame = new Color(0,0,255);
Line p = null;
if (n == 0) {
p = l;
} else {
p = lines.get((n - 1) % N);
}
draw(g, grass, 0, (int) p.Y, grassWidth, 0, (int) l.Y, grassWidth); //(Graphics g, Color c, int x1, int y1, int w1, int x2, int y2, int w2)
draw(g, rumble, (int) p.X, (int) p.Y, (int) (p.W * 2.03), (int) l.X, (int) l.Y, (int) (l.W * 2.03)); //Affects width of rumble
draw(g, road, (int) p.X, (int) p.Y, (int) (p.W * 1.8), (int) l.X, (int) l.Y, (int) (l.W * 1.8));
draw(g, midel, (int) p.X, (int) p.Y, (int) (p.W * 0.78), (int) l.X, (int) l.Y, (int) (l.W * 0.78)); //ADD HERE
draw(g, road, (int) p.X, (int) p.Y, (int) (p.W * 0.7), (int) l.X, (int) l.Y, (int) (l.W* 0.7)); //To cover the gap in between each midel. Must be after to allow it to colour over it
draw(g, car, 965, 927, 125, 965, 1005, 125);
draw(g, tire, 875, 1005, 35, 875, 1050, 35);
draw(g, tire, 1055, 1005, 35, 1055, 1050, 35);
draw(g, stripe, 1050, 965, 30, 870, 980, 30);
draw(g, stripe, 1050, 950, 30, 870, 965, 30);
draw(g, hood, 965, 880, 90, 965, 927, 125);
draw(g, light, 875, 950, 5, 875, 980, 5);
draw(g, light, 890, 950, 5, 890, 980, 5);
draw(g, light, 905, 950, 5, 905, 980, 5);
draw(g, light, 1025, 950, 5, 1025, 980, 5);
draw(g, light, 1040, 950, 5, 1040, 980, 5);
draw(g, light, 1055, 950, 5, 1055, 980, 5);
draw(g, frame, 965, 874, 86, 965, 880, 90);
draw(g, light, 965, 874, 35, 965, 880, 45);
}
}
}
void draw(Graphics g, Color c, int x1, int y1, int w1, int x2, int y2, int w2) {
Graphics g9d = g;
int[] x9Points = { x1 - w1, x2 - w2, x2 + w2, x1 + w1 };
int[] y9Points = { y1, y2, y2, y1 };
int n9Points = 4;
g9d.setColor(c);
g9d.fillPolygon(x9Points, y9Points, n9Points);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new RoadAppMain();
}
});
}
class Line {
double x, y, z;
double X, Y, W;
double scale, curve;
public Line() {
curve = x = y = z = 0;
}
void project(int camX, int camY, int camZ) {
scale = camDist / (z - camZ);
X = (1 + scale * (x - camX)) * grassWidth / 2;
Y = (1 - scale * (y - camY)) * vanishHeight / 2;
W = scale * roadWidth * grassWidth / 2;
}
}
}
How could I implement that movement into my game as well?

Finding Mouse Hovering

I am currently working on creating a game with Java and I wanted to create a hover effect. Whenever the mouse is hovering over a rectangle that i created in the Gui i wanted a boolean to be set to true or false. I am not sure what i did wrong. Here is my code:
package com.tutorial.main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import com.tutorial.main.Game.STATE;
public class Shop extends MouseAdapter{
private Game game;
private Handler handler;
private Random r = new Random();
private HUD hud;
private boolean enough_maxhealth = true;
private boolean enough_speed = true;
private boolean enough_frozen = true;
private boolean enough_blast = true;
private boolean health_info = false;
private boolean speed_info = false;
private boolean freeze_info = false;
private boolean blast_info = false;
//static to show other classes what is upgraded, can be implemented so it won't have to in future
public static int[] upgrades = {0,0,3,0};
private static int[] prices = {1,2,1,5};
//way program is built, coin has to be static
public static int COIN = 0;
private int shop_timer = 0;
//check if this is needed
private int timer_frozen_upgrade = 100;
public Shop(Game game, Handler handler, HUD hud){
this.game = game;
this.handler = handler;
this.hud = hud;
}
public void mousePressed(MouseEvent e){
int mx = e.getX();
int my = e.getY();
}
public void mouseReleased(MouseEvent e){
int mx = e.getX();
int my = e.getY();
//Shop buttons
if(game.gameState == STATE.Shop){
//back button
if(mouseOver(mx, my, 210, 350, 200, 64)){
game.gameState = STATE.Game;
return;
}
//health upgrade
if(mouseOver(mx, my, 80, 110, 150, 50)){
// Cost 1 coins for + 10 health
if (COIN >= prices[0]){
//checks if maxed, 3 is the max
if (upgrades[0] < 3){
//updates upgrades
upgrades[0] += 1;
//adds 10 health to players max health
HUD.MAX_HEALTH += 10;
COIN -= prices[0];
}
}else{
//set a message to be shown for 40 ticks
shop_timer = 40;
//signifies that you don't have enough
enough_maxhealth = false;
}
}
//back button
if(mouseOver(mx, my, 230, 380, 200, 64)){
game.gameState = STATE.Game;
}
//speed upgrade
if(mouseOver(mx, my, 80, 200, 150, 50)){
if(COIN >= prices[1]){
if(upgrades[1] < 3){
for(int i = 0; i < handler.object.size(); i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ID.Player){
//increases player default Vx and Vy by 108%
tempObject.setVx_Default(tempObject.getVx_Default() * 1.08f);
tempObject.setVy_Default(tempObject.getVy_Default() * 1.08f);
//costs 2 coins
COIN -= prices[1];
upgrades[1] += 1;
}
}
}
}else{
//set a message to be shown for 40 ticks
shop_timer = 40;
enough_speed = false;
}
}
//Freeze enemies
/*
if(mouseOver(mx, mx, Game.WIDTH - 230, 110, 150, 50)){
System.out.println("Pressed buy");
if(COIN >= 2){
if(upgrades[2] < 3){
upgrades[2] += 1;
System.out.println("Bought frozen");
}
}
}*/
//freeze
if(mouseOver(mx, my, Game.WIDTH - 230, 110, 150, 50)){
if(COIN >= prices[2]){
//if(upgrades[2] < 3){
for(int i = 0; i < handler.object.size(); i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ID.Player){
//increases player default Vx and Vy by 108%
COIN -= prices[2];
upgrades[2] += 1;
}
//}
}
}else{
//set a message to be shown for 40 ticks
shop_timer = 40;
enough_frozen = false;
}
}
//blast
if(mouseOver(mx, my, Game.WIDTH - 230, 200, 150, 50)){
if(COIN >= prices[3]){
if(upgrades[3] < 2){
for(int i = 0; i < handler.object.size(); i++){
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ID.Player){
//increases player default Vx and Vy by 108%
COIN -= prices[3];
upgrades[3] += 1;
}
}
}
}else{
//set a message to be shown for 40 ticks
shop_timer = 40;
enough_blast = false;
}
}
}
}
//returns true or false depends of mouse is over certain cordinates
private boolean mouseOver(int mx, int my, int x, int y, int width, int height){
if(mx > x && mx < x + width){
if (my > y && my < y + height){
return true;
}else return false;
}else return false;
}
//checks if the mouse has entered one of the rectangles
public void mouseEntered(MouseEvent e){
int mx = e.getX();
int my = e.getY();
//Shop buttons
if(game.gameState == STATE.Shop){
//health box
if(mouseOver(mx, my, 80, 110, 150, 50)){
health_info = true;
}else{
health_info = false;
}
//speed box
if(mouseOver(mx, my, 80, 200, 150, 50)){
speed_info = true;
}else{
speed_info = false;
}
//freeze box
if(mouseOver(mx, my, 410, 110, 150, 50)){
freeze_info = true;
}
else freeze_info = false;
//blast box
if(mouseOver(mx, my, 410, 200, 150, 50)){
blast_info = true;
}else{
blast_info = false;
}
}
}
//checks if the mouse has exited one of the rectangles
public void mouseExited(MouseEvent e){
int mx = e.getX();
int my = e.getY();
//Shop buttons
if(game.gameState == STATE.Shop){
//health box
if(mouseOver(mx, my, 80, 110, 150, 50)){
health_info = false;
}else{
health_info = true;
}
//speed box
if(mouseOver(mx, my, 80, 200, 150, 50)){
speed_info = false;
}else{
speed_info = true;
}
//freeze box
if(mx > 110 && mx < 410){
if(my > 50 && my < 150){
freeze_info = false;
System.out.println("exit in");
}
else{
freeze_info = true;
System.out.println("exit left");
}
}
else{
freeze_info = true;
System.out.println("exit left");
}
//blast box
if(mouseOver(mx, my, 410, 200, 150, 50)){
blast_info = false;
}else{
blast_info = true;
}
}
}
public void tick(){
//reduces shop timer that shows message
if(shop_timer >= 0){
shop_timer--;
}
}
public void render(Graphics g){
//try to simplify/optimize
//Shop System
if(game.gameState == STATE.Shop){
//creates fonts for future reference
Font fnt = new Font("arial", 1, 50);
Font fnt2 = new Font("arial", 1, 20);
Font fnt3 = new Font("arial", 1, 17);
Font fnt4 = new Font("arial", 1, 11);
Font fnt5 = new Font("arial", 1, 9);
Font fnt6 = new Font("arial", 1, 40);
g.setColor(Color.black);
g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);
g.setColor(Color.gray);
g.setFont(fnt);
g.drawString("Shop", 250, 50);
//Displays Health
g.setColor(Color.gray);
g.fillRect(15, 15, 200, 32);
g.setColor(new Color(75, (int) HUD.greenValue, 0));
g.fillRect(15,15, (int) HUD.HEALTH * 2, 32);
g.setColor(Color.white);
g.drawRect(15, 15, 200, 32);
g.setFont(fnt2);
g.setColor(Color.white);
//Health upgrade
g.drawRect(80, 110, 150, 50);
g.drawString("Max Health", 90, 140);
g.setColor(Color.gray);
if(upgrades[0] >= 1){
g.setColor(Color.green);
}
g.fillRect(82, 170, 44, 14);
if(upgrades[0] >= 2){
g.setColor(Color.green);
}else{
g.setColor(Color.gray);
}
g.fillRect(134, 170, 44, 14);
if(upgrades[0] >= 3){
g.setColor(Color.green);
}else{
g.setColor(Color.gray);
}
g.fillRect(186, 170, 44, 14);
//Speed upgrade
g.setColor(Color.white);
g.drawRect(80, 200, 150, 50);
g.setColor(Color.gray);
if(upgrades[1] >= 1){
g.setColor(Color.green);
}
g.fillRect(82, 260, 44, 16);
if(upgrades[1] >= 2){
g.setColor(Color.green);
}else{
g.setColor(Color.gray);
}
g.fillRect(134, 260, 44, 16);
if(upgrades[1] >= 3){
g.setColor(Color.green);
}else{
g.setColor(Color.gray);
}
g.fillRect(186, 260, 44, 16);
g.setColor(Color.white);
g.setFont(fnt3);
g.drawString("Speed Upgrade", 82, 230);
//freeze enemy effect
g.drawRect(Game.WIDTH - 230, 110, 150, 50);
g.setColor(Color.white);
g.setFont(fnt3);
g.drawString("Freze Enemy", Game.WIDTH - 215, 140);
g.setFont(fnt4);
if(freeze_info){
g.drawString("Press 'f' to slow enemies", Game.WIDTH - 230, 173 );
g.drawString("(Can only be used once)", Game.WIDTH - 228, 185);
}
g.setFont(fnt6);
g.setColor(Color.red);
g.drawString("" + upgrades[2], 590, 150);
//blast for some odd ticks
g.setColor(Color.white);
g.drawRect(Game.WIDTH - 230, 200, 150, 50);
//g.setColor(Color.gray);
//g.fillRect(80, 210, 33, 10);
g.setFont(fnt2);
g.drawString("Blast", Game.WIDTH - 190, 230);
g.setFont(fnt4);
g.drawString("Press 'ctrl' for Imunity", Game.WIDTH - 230, 263 );
g.drawString("(Can only be used once)", Game.WIDTH - 228, 278);
g.setFont(fnt6);
g.setColor(Color.red);
g.drawString("" + upgrades[3], 590, 240);
g.setFont(fnt);
//back button
g.setColor(Color.white);
g.drawRect(230, 380, 200, 64);
g.drawString("Back", 260, 430);
if(!enough_maxhealth || !enough_speed || !enough_frozen || !enough_blast){
if(shop_timer > 0){
System.out.println(shop_timer);
g.setFont(fnt2);
g.setColor(Color.white);
if(!enough_maxhealth){
g.drawString("You need " + (prices[0] - COIN) + " coins more", 190, 90);
}else if(!enough_speed){
g.drawString("You need " + (prices[1] - COIN) + " coins more", 190, 90);
}else if(!enough_frozen){
g.drawString("You need " + (prices[2] - COIN) + " coins more", 190, 90);
}else if(!enough_blast){
g.drawString("You need " + (prices[3] - COIN) + " coins more", 190, 90);
}
}else{
enough_maxhealth = true;
enough_speed = true;
enough_frozen = true;
enough_blast = true;
}
}
}
}
}
I tried to change some things with the freeze to see if it would effect the way it worked, but it still isn't properly working. The boolean for freeze is set to true in MouseExit whenever i leave the GUI, but i cannot make it correspond to the rectangle.
The class Rectangle2D has methods for testing collisions with other rectangles and/or a single point.
Tests if the specified coordinates are inside the boundary of the
Shape
contains(double x, double y)
and
Tests if the interior of the Shape entirely contains the specified
rectangular area.
contains(double x, double y, double w, double h)
I'd suggest refactoring your code to use these instead, and write some unit tests if you don't have any to prove it works.
Extend from this class to inherit the functionality for your objects
******************** EDITS ********************
class SomeObjectToRepresent extends Rectangle2D {
#override
public boolean contains(x, y){
boolean b = super.contains(x, y);
// change the state based on b
}
}
Something like the above but not exactly, just trying to point you in the right direction.
As for mouse events I would recommend using the MouseMotionListener
instead and use mouseMoved event and check your components, set their state based on whether the mouse is within bounds of your component by updating a list of components in your application. A component manager can do this or your JFrame can whenever a mouse event is obtained.
It's more brutish to do this but it's very easy to implement and I doubt you'll have many components.
Since every time the mouse moves that list will be updated or at least touched on.
I'm trying to find out where this was but you may need to add your mouse listener twice, one for motion and another for just the original. I know that sounds strange but I vaguely remember this being a problem for me too when events were not firing.
Rectangle2D

Change Hue of Picture in Graphics

Ok so i need help changing the hue of this slider. I cant seem to figure it out. Please no #override. I need something that will run on Ready to Program. The hue will change back to normal when the slider is back at 0. I dont need to get too complex. Just a simple Hue slider will be great. Thanks!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import javax.swing.event.*;
import java.applet.*;
public class test extends Applet implements ActionListener, ChangeListener
{
//Widgets, Panels
JSlider slider;
Panel flow;
int colorr;
int colorg;
int colorb;
int stars;
//House Coordinates, initialized to 1. (Top Right and No Scaling)
public void init ()
{ //Set Up Input Fields for House Coordinates
resize (380, 240);
setBackground (new Color (102, 179, 255));
slider = new JSlider ();
slider.setValue (0);
slider.setBackground (new Color (102, 179, 255));
slider.setForeground (Color.white);
slider.setMajorTickSpacing (20);
slider.setMinorTickSpacing (5);
slider.setPaintTicks (true);
slider.addChangeListener (this);
//Set up layout, add widgets
setLayout (new BorderLayout ());
flow = new Panel (new FlowLayout ());
flow.add (slider);
add (flow, "South");
}
public void paint (Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
Color color1 = getBackground ();
Color color2 = color1.darker ();
int x = getWidth ();
int y = getHeight () - 30;
GradientPaint gp = new GradientPaint (
0, 0, color1,
0, y, color2);
g2d.setPaint (gp);
g2d.fillRect (0, 0, x, y);
stars (10, 10);
}
public void stars (int x, int y)
{
Graphics g = getGraphics ();
//sun
g.setColor (new Color (139, 166, 211));
g.fillOval (-200, 170, 1000, 400);
g.setColor (new Color (206, 75, 239));
g.fillOval (x, y, 10, 10); //First medium star
g.drawLine (x + 5, y, x + 5, 0);
g.drawLine (x, y + 5, 0, y + 5);
g.drawLine (x + 5, y + 10, x + 5, y + 20);
g.drawLine (x + 10, y + 5, x + 20, y + 5);
g.fillOval (x + 80, y + 30, 12, 12); //Middle medium star
g.drawLine (x + 86, y + 30, x + 86, y + 18);
g.drawLine (x + 80, y + 36, x + 68, y + 36);
g.drawLine (x + 92, y + 36, x + 104, y + 36);
g.drawLine (x + 86, y + 42, x + 86, y + 52);
colorr = (int) (Math.random () * 255) + 1;
colorg = (int) (Math.random () * 255) + 1;
colorb = (int) (Math.random () * 255) + 1;
int randomx = (int) (Math.random () * 300) + 10;
int randomy = (int) (Math.random () * 150) + 10;
stars = 50; //Change for more stars
int ax[] = new int [stars];
int ay[] = new int [stars];
for (int i = 0 ; i < stars ; i++)
{
g.setColor (new Color (colorr, colorg, colorb));
colorr = (int) (Math.random () * 255) + 1;
colorg = (int) (Math.random () * 255) + 1;
colorb = (int) (Math.random () * 255) + 1;
while ((randomx > 88 && randomx < 116) && (randomy < 65 && randomy > 15))
{
randomx = (int) (Math.random () * 300) + 10;
randomy = (int) (Math.random () * 150) + 10;
}
while ((randomx > 0 && randomx < 25) && (randomy > 5 && randomy < 35))
{
randomx = (int) (Math.random () * 300) + 10;
randomy = (int) (Math.random () * 150) + 10;
}
g.drawOval (randomx, randomy, 5, 5);
randomx = (int) (Math.random () * 300) + 10;
randomy = (int) (Math.random () * 150) + 10;
}
g.setColor (Color.white);
g.drawLine (320, 0, 315, 40);
g.drawLine (320, 0, 325, 40);
g.drawLine (320, 120, 315, 80);
g.drawLine (320, 120, 325, 80);
g.drawLine (260, 60, 300, 55);
g.drawLine (260, 60, 300, 65);
g.drawLine (380, 60, 340, 55);
g.drawLine (380, 60, 340, 65);
fillGradOval (280, 20, 80, 80, new Color (254, 238, 44), new Color (255, 251, 191), g);
g.setColor (new Color (255, 251, 191));
fillGradOval (300, 40, 40, 40, new Color (255, 251, 191), new Color (254, 238, 44), g);
}
public void fillGradOval (int X, int Y, int H2, int W2, Color c1, Color c2, Graphics g)
{
g.setColor (c1);
g.fillOval (X, Y, W2, H2);
Color Gradient = c1;
float red = (c2.getRed () - c1.getRed ()) / (W2 / 2);
float blue = (c2.getBlue () - c1.getBlue ()) / (W2 / 2);
float green = (c2.getGreen () - c1.getGreen ()) / (W2 / 2);
int scale = 1;
int r = c1.getRed ();
int gr = c1.getGreen ();
int b = c1.getBlue ();
while (W2 > 10)
{
r = (int) (r + red);
gr = (int) (gr + green);
b = (int) (b + blue);
Gradient = new Color (r, gr, b);
g.setColor (Gradient);
W2 = W2 - 2 * scale;
H2 = H2 - 2 * scale;
X = X + scale;
Y = Y + scale;
g.fillOval (X, Y, W2, H2);
}
}
public void actionPerformed (ActionEvent e)
{
}
public void stateChanged (ChangeEvent e)
{
JSlider source = (JSlider) e.getSource ();
if (!source.getValueIsAdjusting ())
{
}
}
}
I wasn't sure what 'color' you were referring to, so I made some guesses. Here is an hybrid application/applet (much easier for development and testing) that links the color of the bottom panel, as well as the bottom color of the gradient paint, to a hue as defined using the slider.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/* <applet code=HueSlider width=380 height=240></applet> */
public class HueSlider extends JApplet
{
public void init() {
add(new HueSliderGui());
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
HueSliderGui hsg = new HueSliderGui();
JOptionPane.showMessageDialog(null, hsg);
}
};
SwingUtilities.invokeLater(r);
}
}
class HueSliderGui extends JPanel implements ChangeListener {
//Widgets, Panels
JSlider slider;
JPanel flow;
int colorr;
int colorg;
int colorb;
Color bg = new Color (102, 179, 255);
int stars;
//House Coordinates, initialized to 1. (Top Right and No Scaling)
Dimension prefSize = new Dimension(380, 240);
HueSliderGui() {
initGui();
}
public void initGui()
{
//Set Up Input Fields for House Coordinates
// an applet size is set in HTML
//resize (380, 240);
setBackground (bg);
slider = new JSlider ();
slider.setValue (0);
slider.setBackground (new Color (102, 179, 255));
slider.setForeground (Color.white);
slider.setMajorTickSpacing (20);
slider.setMinorTickSpacing (5);
slider.setPaintTicks (true);
slider.addChangeListener (this);
//Set up layout, add widgets
setLayout (new BorderLayout ());
flow = new JPanel (new FlowLayout ());
flow.add (slider);
add (flow, "South");
validate();
}
#Override
public Dimension getPreferredSize() {
return prefSize;
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Color color1 = getBackground ();
Color color2 = color1.darker ();
int x = getWidth ();
int y = getHeight () - 30;
GradientPaint gp = new GradientPaint (
0, 0, color1,
0, y, flow.getBackground());
g2d.setPaint (gp);
g2d.fillRect (0, 0, x, y);
stars (10, 10, g2d);
}
public void stars (int x, int y, Graphics g)
{
// Graphics g = getGraphics (); we should never call getGraphics
//sun
g.setColor (new Color (139, 166, 211));
g.fillOval (-200, 170, 1000, 400);
g.setColor (new Color (206, 75, 239));
g.fillOval (x, y, 10, 10); //First medium star
g.drawLine (x + 5, y, x + 5, 0);
g.drawLine (x, y + 5, 0, y + 5);
g.drawLine (x + 5, y + 10, x + 5, y + 20);
g.drawLine (x + 10, y + 5, x + 20, y + 5);
g.fillOval (x + 80, y + 30, 12, 12); //Middle medium star
g.drawLine (x + 86, y + 30, x + 86, y + 18);
g.drawLine (x + 80, y + 36, x + 68, y + 36);
g.drawLine (x + 92, y + 36, x + 104, y + 36);
g.drawLine (x + 86, y + 42, x + 86, y + 52);
colorr = (int) (Math.random () * 255) + 1;
colorg = (int) (Math.random () * 255) + 1;
colorb = (int) (Math.random () * 255) + 1;
int randomx = (int) (Math.random () * 300) + 10;
int randomy = (int) (Math.random () * 150) + 10;
stars = 50; //Change for more stars
int ax[] = new int [stars];
int ay[] = new int [stars];
for (int i = 0 ; i < stars ; i++)
{
g.setColor (new Color (colorr, colorg, colorb));
colorr = (int) (Math.random () * 255) + 1;
colorg = (int) (Math.random () * 255) + 1;
colorb = (int) (Math.random () * 255) + 1;
while ((randomx > 88 && randomx < 116) && (randomy < 65 && randomy > 15))
{
randomx = (int) (Math.random () * 300) + 10;
randomy = (int) (Math.random () * 150) + 10;
}
while ((randomx > 0 && randomx < 25) && (randomy > 5 && randomy < 35))
{
randomx = (int) (Math.random () * 300) + 10;
randomy = (int) (Math.random () * 150) + 10;
}
g.drawOval (randomx, randomy, 5, 5);
randomx = (int) (Math.random () * 300) + 10;
randomy = (int) (Math.random () * 150) + 10;
}
g.setColor (Color.white);
g.drawLine (320, 0, 315, 40);
g.drawLine (320, 0, 325, 40);
g.drawLine (320, 120, 315, 80);
g.drawLine (320, 120, 325, 80);
g.drawLine (260, 60, 300, 55);
g.drawLine (260, 60, 300, 65);
g.drawLine (380, 60, 340, 55);
g.drawLine (380, 60, 340, 65);
fillGradOval (280, 20, 80, 80, new Color (254, 238, 44), new Color (255, 251, 191), g);
g.setColor (new Color (255, 251, 191));
fillGradOval (300, 40, 40, 40, new Color (255, 251, 191), new Color (254, 238, 44), g);
}
public void fillGradOval (int X, int Y, int H2, int W2, Color c1, Color c2, Graphics g)
{
g.setColor (c1);
g.fillOval (X, Y, W2, H2);
Color Gradient = c1;
float red = (c2.getRed () - c1.getRed ()) / (W2 / 2);
float blue = (c2.getBlue () - c1.getBlue ()) / (W2 / 2);
float green = (c2.getGreen () - c1.getGreen ()) / (W2 / 2);
int scale = 1;
int r = c1.getRed ();
int gr = c1.getGreen ();
int b = c1.getBlue ();
while (W2 > 10)
{
r = (int) (r + red);
gr = (int) (gr + green);
b = (int) (b + blue);
Gradient = new Color (r, gr, b);
g.setColor (Gradient);
W2 = W2 - 2 * scale;
H2 = H2 - 2 * scale;
X = X + scale;
Y = Y + scale;
g.fillOval (X, Y, W2, H2);
}
}
public void stateChanged (ChangeEvent e)
{
JSlider source = (JSlider) e.getSource ();
if (!source.getValueIsAdjusting ())
{
int i = source.getValue();
System.out.println(i);
float[] hsb = Color.RGBtoHSB(bg.getRed(),bg.getGreen(),bg.getBlue(),null);
int colorHue = Color.HSBtoRGB((float)i/100f, hsb[1], hsb[2]);
Color c = new Color(colorHue);
flow.setBackground(c);
this.repaint();
}
}
}

Plot the sine and cosine functions

I'm currently having some problems regarding my homework.
Here's the Exercise:
(Plot the sine and cosine functions) Write a program that plots the sine function in red and the cosine function in blue.
hint: The Unicode for Pi is \u03c0. To display -2Pi, use g.drawString("-2\u03c0", x, y). For a trigonometric function like sin(x), x is in radians. Use the following loop to add the points to a polygon p
for (int x = -170; x <= 170; x++) {
p.addPoint(x + 200, 100 - (int)(50 * Math.sin((x / 100.0) * 2 * Math.PI)));
-2Pi is at (100, 100), the center of the axis is at (200, 100), and 2Pi is at (300, 100)
Use the drawPolyline method in the Graphics class to connect the points.
Okay, so the sin function I have is a little different from the one in the exercise but it works so it shouldn't be a problem. The cosine function on the other hand, I'm having trouble finding the code for it so I don't have that in my program.
What I also need to do is place -Pi and Pi on the graph on their respectable places.
So, here's the code.
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Exercise13_12 extends JFrame {
public Exercise13_12() {
setLayout(new BorderLayout());
add(new DrawSine(), BorderLayout.CENTER);
}
public static void main(String[] args) {
Exercise13_12 frame = new Exercise13_12();
frame.setSize(400, 300);
frame.setTitle("Exercise13_12");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class DrawSine extends JPanel {
double f(double x) {
return Math.sin(x);
}
double g(double y) {
return Math.cos(y);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(10, 100, 380, 100);
g.drawLine(200, 30, 200, 190);
g.drawLine(380, 100, 370, 90);
g.drawLine(380, 100, 370, 110);
g.drawLine(200, 30, 190, 40);
g.drawLine(200, 30, 210, 40);
g.drawString("X", 360, 80);
g.drawString("Y", 220, 40);
Polygon p = new Polygon();
for (int x = -170; x <= 170; x++) {
p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
* Math.PI)));
}
g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
g.drawString("-2\u03c0", 95, 115);
g.drawString("2\u03c0", 305, 115);
g.drawString("0", 200, 115);
}
}
}
If anyone have the time to help me out I would be very grateful.
Try this:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Exercise13_12 extends JFrame {
public Exercise13_12() {
setLayout(new BorderLayout());
add(new DrawSine(), BorderLayout.CENTER);
}
public static void main(String[] args) {
Exercise13_12 frame = new Exercise13_12();
frame.setSize(400, 300);
frame.setTitle("Exercise13_12");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class DrawSine extends JPanel {
double f(double x) {
return Math.sin(x);
}
double gCos(double y) {
return Math.cos(y);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(10, 100, 380, 100);
g.drawLine(200, 30, 200, 190);
g.drawLine(380, 100, 370, 90);
g.drawLine(380, 100, 370, 110);
g.drawLine(200, 30, 190, 40);
g.drawLine(200, 30, 210, 40);
g.drawString("X", 360, 80);
g.drawString("Y", 220, 40);
Polygon p = new Polygon();
Polygon p2 = new Polygon();
for (int x = -170; x <= 170; x++) {
p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
* Math.PI)));
}
for (int x = -170; x <= 170; x++) {
p2.addPoint(x + 200, 100 - (int) (50 * gCos((x / 100.0) * 2
* Math.PI)));
}
g.setColor(Color.red);
g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
g.drawString("-2\u03c0", 95, 115);
g.drawString("2\u03c0", 305, 115);
g.drawString("0", 200, 115);
g.setColor(Color.blue);
g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);
}
}
}
Basically it's the same code all over, but you need a new polygon to draw it. And then I set the color using the setColor() function of the Graphics.
You can add this to your paintComponent method:
//Draw pi and -pi
g.drawString("-\u03c0", 147, 100);
g.drawString("\u03c0", 253, 100);
//Create a new polygon
Polygon p2 = new Polygon();
//Add the points of the cosine
for (int x = -170; x <= 170; x++) {
p2.addPoint(x + 200, 100 - (int) (50 * g((x / 100.0) * 2
* Math.PI)));
}
//Draw the function
g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);
With that you can have the results that you need.
Okay so now that the program is complete i ended up with this.
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Exercise13_12 extends JFrame {
public Exercise13_12() {
setLayout(new BorderLayout());
add(new DrawSine(), BorderLayout.CENTER);
}
public static void main(String[] args) {
Exercise13_12 frame = new Exercise13_12();
frame.setSize(400, 300);
frame.setTitle("Exercise13_12");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class DrawSine extends JPanel {
double f(double x) {
return Math.sin(x);
}
double gCos(double y) {
return Math.cos(y);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(10, 100, 380, 100);
g.drawLine(200, 30, 200, 190);
g.drawLine(380, 100, 370, 90);
g.drawLine(380, 100, 370, 110);
g.drawLine(200, 30, 190, 40);
g.drawLine(200, 30, 210, 40);
g.drawString("X", 360, 80);
g.drawString("Y", 220, 40);
Polygon p = new Polygon();
Polygon p2 = new Polygon();
for (int x = -170; x <= 170; x++) {
p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
* Math.PI)));
}
for (int x = -170; x <= 170; x++) {
p2.addPoint(x + 200, 100 - (int) (50 * gCos((x / 100.0) * 2
* Math.PI)));
}
g.setColor(Color.red);
g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
g.drawString("-2\u03c0", 95, 115);
g.drawString("-\u03c0", 147, 115);
g.drawString("\u03c0", 253, 115);
g.drawString("2\u03c0", 305, 115);
g.drawString("0", 200, 115);
g.setColor(Color.blue);
g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);
}
}
}
For anyone that might have the same problem as me later on.
And thanks guys for helping me out, always grateful.
check it out .....
public class GuiBasicstest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic herei
guiBasics gb = new guiBasics();
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.add(gb);
jf.setVisible(true);
}
}
................................................................................
package guibasics;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* #author ALI
*/
public class guiBasics extends JPanel{
//Graphics g = null;
public void paintComponent(Graphics g){
//super.paintComponent(g);
for(double i = 0; i<200;i++){
double y= Math.sin(i);
draw(i,y,g);
}
}
private void draw(double x , double y,Graphics g ){
double x1, y1;
x+=10;
y+=10;
x*=10;
y*=30;
x1=x+1;
y1=y+1;
Double d = new Double(x);
int a = d.intValue();
int b = (new Double(y)).intValue();
int c = (new Double(x1)).intValue();
int e = (new Double(y1)).intValue();
g.drawLine(a, b, c, e);
}
}

Categories

Resources