Traffic light semaphore using threads - java

I am trying to build a traffic light semaphore using threads but I can't get right.Semaphore should display red, yellow, green with break between colors. Any help is much appreciated.
Thanks.
Here is my code...
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StopTheLights extends JFrame implements ActionListener {
JButton start;
JButton stop;
JPanel panel;
Boolean flag;
public StopTheLights(String title) {
Container c = getContentPane();
start = new JButton("Start");
stop = new JButton("Stop");
start.addActionListener(this);
stop.addActionListener(this);
panel= new JPanel();
panel.add(start);
panel.add(stop);
c.add(panel, BorderLayout.SOUTH);
setSize(300, 450);
setVisible(true);
setLocation(200,200);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.black);
g.drawOval(50,50,100,100);
g.drawOval(50,155,100,100);
g.drawOval(50,260,100,100);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == start){
flag = true;
new ThreadExtend(this).start();
}else{
flag = false;
}
}
public class ThreadExtend extends Thread {
Graphics g;
JFrame frame;
public ThreadExtend(JFrame frame){
this.frame = frame;
g = frame.getGraphics();
}
public void run(){
while(flag) {
try{
paintRed(g);
if(!flag) {
break;
}
Thread.sleep(1000);
paintAmber(g);
if(!flag) {
break;
}
Thread.sleep(1000);
paintGreen(g);
if(!flag){
break;
}
}catch(InterruptedException e) {
}
}
}
}
public void paintRed(Graphics g){
g.setColor(new Color(255,0,0));
g.fillOval(52, 52, 96, 96);
g.fillOval(52, 52, 96, 96);
g.fillOval(52, 262, 96, 96);
}
public void paintAmber(Graphics g){
g.setColor(new Color(250,170,0));
g.fillOval(52, 157, 96, 96);
g.fillOval(52, 52, 96, 96);
g.fillOval(52, 262, 96, 96);
}
public void paintGreen(Graphics g){
g.setColor(new Color(0,250,0));
g.fillOval(52, 262, 96, 96);
g.fillOval(52, 52, 96, 96);
g.fillOval(52, 157, 96, 96);
}
public static void main(String[] args) {
new StopTheLights();
}
}

There are several problems here.
Your Thread does not wait after showing Green light.
You paint all the lights in all the methods calls, which is not needed.
You don't change the color when "closing" other lights.
I did some fixing in your code, and now it works:
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StopTheLights extends JFrame implements ActionListener {
JButton start;
JButton stop;
Boolean i;
//create constructor method
public StopTheLights(String title) {
// create Start button
start = new JButton("Start");
//create Stop button
stop = new JButton("Stop");
//add Action listeners
start.addActionListener(this);
stop.addActionListener(this);
//create new Button Panel
JPanel buttonPanel = new JPanel();
buttonPanel.add(start);
buttonPanel.add(stop);
Container c = getContentPane();
c.add(buttonPanel, BorderLayout.SOUTH);
setSize(200, 425);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.black);
g.fillOval(50,50,100,100);
g.fillOval(50,155,100,100);
g.fillOval(50,260,100,100);
}
public static void main(String[] args) {
new StopTheLights("Stop The Lights");
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == start){
i = true;
new ThreadExtend(this).start();
}else{
i = false;
}
}
public class ThreadExtend extends Thread {
Graphics g;
JFrame frame;
public ThreadExtend(JFrame frame){
this.frame = frame;
g = frame.getGraphics();
}
public void run(){
while(i) {
try{
red(g);
if(!i) {
break;
}
Thread.sleep(1000);
amber(g);
if(!i) {
break;
}
Thread.sleep(1000);
green(g);
if(!i){
break;
}
Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void red(Graphics g){
g.setColor(Color.black);
g.fillOval(52, 262, 96, 96);
g.setColor(new Color(255,0,0));
g.fillOval(52, 52, 96, 96);
}
public void amber(Graphics g){
g.setColor(Color.black);
g.fillOval(52, 52, 96, 96);
g.setColor(new Color(250,170,0));
g.fillOval(52, 157, 96, 96);
}
public void green(Graphics g){
g.setColor(Color.black);
g.fillOval(52, 157, 96, 96);
g.setColor(new Color(0,250,0));
g.fillOval(52, 262, 96, 96);
}
}

Related

How to show animated GIFs?

First of all I am making a minigame with the Five Nights At Freddy's graphics and jump scares. I already know how to import and draw a picture (png and etc). The only one I don't know how to import, are gifs.
Here Is My Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Minigame extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
GameEvents gameEvents = new GameEvents();
Timer gameTimer = new Timer(1, gameEvents);
int i = 0;
int horizontalposition = 500;
int verticalposition = 500;
BufferedImage Picture;
BufferedImage Picture2;
BufferedImage Picture3;
BufferedImage Picture4;
BufferedImage Picture5;
BufferedImage Picture6;
//Don't forget to declare your variables!
Minigame()
{
gameTimer.start();
this.addKeyListener(gameEvents);
try
{
Picture = ImageIO.read(getClass().getResource("Child.gif"));
Picture2 = ImageIO.read(getClass().getResource("Freddycake_Gif.gif"));
Picture3 = ImageIO.read(getClass().getResource("Purple_man.png"));
Picture4 = ImageIO.read(getClass().getResource("Cake_Child_Idle.png"));
Picture5 = ImageIO.read(getClass().getResource("Cake_Child.gif"));
//The format for this is Picture = ImageIO.read(getClass().getResource("NameOfFile.typeoffile"));
}
catch (IOException e)
{
System.out.println("Pictures failed to load");
}
}
#Override
protected void paintComponent(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,this.getWidth(), this.getHeight());
///g.drawImage(Picture, horizontalposition, verticalposition, 100, 150, null);
g.drawImage(Picture, 200, 10, 70, 100, null);
g.drawImage(Picture, 200, 100, 70, 100, null);
g.drawImage(Picture, 200, 200, 70, 100, null);
g.drawImage(Picture, 200, 300, 70, 100, null);
g.drawImage(Picture, 200, 400, 70, 100, null);
g.drawImage(Picture, 200, 500, 70, 100, null);
g.drawImage(Picture2, horizontalposition, verticalposition, 100, 150, null);
g.drawImage(Picture3, 1100, 50, 100, 150, null);
g.drawImage(Picture4, 900, 50, 100, 150, null);
//g.drawImage(Picture5, 900, 50, 100, 150, null);
}
public class GameEvents implements ActionListener, KeyListener
{
#Override
public void actionPerformed(ActionEvent arg0)
{
repaint();
}
#Override
public void keyPressed(KeyEvent key) //stuff inside here happens when a key is pressed
{
if(key.getKeyChar()=='d')
{
horizontalposition=horizontalposition+50;
}
if(key.getKeyChar()=='s')
{
verticalposition=verticalposition+50;
}
if(key.getKeyChar()=='w')
{
verticalposition=verticalposition-50;
}
if(key.getKeyChar()=='a')
{
horizontalposition=horizontalposition-50;
}
if(horizontalposition<0)
{
horizontalposition=0;
}
System.out.println(key.getKeyChar());
System.out.println('d');
}
#Override
public void keyReleased(KeyEvent arg0) {
}
#Override
public void keyTyped(KeyEvent arg0) {
}
}
public static void main(String[] args)
{
JFrame f = new JFrame("Java Graphics Example Project");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Minigame p = new Minigame();
f.setSize(1500,700);
f.add(p);
f.setVisible(true);
p.requestFocusInWindow();
}
}
Any yes, I already know how to do g.drawImage();
Look up this answer response: Show animated GIF
If you don't feel like it, I'll just put the code of the person who replied here. However, they explain it there and really go in detail.
THIS IS THE CODE FROM STACKER WHO EXPLAINED IT ON THE LINK I GAVE YOU
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("<URL to your Animated GIF>");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
JFrame f = new JFrame("Animation");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

JButton not shown on screen

Im trying to make an application that will change the state of a traffic light in the click of a button.
My code: Main
import javax.swing.*;
public class PP416
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Traffic light");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TrafficPanel());
frame.pack();
frame.setVisible(true);
}
}
JPanel Class:
import javax.swing.*;
import java.awt.*;
import java.awt.Event;
public class TrafficPanel extends JPanel
{
private JButton button;
private int indicator = 0; // Light is off
public TrafficPanel()
{
button = new JButton("Change");
this.add(button);
}
public void paint(Graphics g)
{
if (indicator == 0)
{
g.drawOval(30, 40, 30, 30);
g.drawOval(30, 70, 30, 30);
g.drawOval(30, 100, 30, 30);
}
}
}
the button just not appearing , just the ovalls.
anyone can help me with this?
Don't override paint but rather paintComponent and Most important, call the super's method. Your lack of a super call may be preventing your JPanel from drawing its child components well.
e.g.,
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (indicator == 0) {
g.drawOval(30, 40, 30, 30);
g.drawOval(30, 70, 30, 30);
g.drawOval(30, 100, 30, 30);
}
}

How to restart?

I have this code and I want the user to be able to restart the game by pressing R, but I do not know how to do this, I've tried to call the main method again, which obviously didn't work, but I don't know any other way to restart it, this is the main class code:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JPanel implements KeyListener {
long startT = System.currentTimeMillis();
long elapsedTimeMillis;
float elapsedT;
private Player player;
private Stage stage;
private boolean isGameOver = false;
private EnemyManager manager;
public Main() {
setSize(800,600);
setPreferredSize(new Dimension(800,600));
setFocusable(true);
addKeyListener(this);
stage = new Stage();
player = new Player(this, 200, 500);
manager = new EnemyManager(this, 10);
}
#Override
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
if(!isGameOver){
player.draw(g);
manager.draw(g);
stage.draw(g);
long elapsedTimeMillis = System.currentTimeMillis() - startT;
elapsedT = elapsedTimeMillis/1000F;
g.setColor(Color.white);
g.setFont(new Font("Century Gothic", Font.BOLD, 24));
g.drawString("You're alive for: " + elapsedT, 400, 50);
}else {
g.setFont(new Font("Century Gothic", Font.BOLD, 55));
g.setColor(Color.RED);
g.drawString("Game Over" , 250, 150);
g.setColor(Color.ORANGE);
g.setFont(new Font("Century Gothic", Font.BOLD, 24));
g.drawString("You lasted for: ", 300, 250);
g.drawString("Press to restart", 300, 300);
g.setColor(Color.white);
g.drawString("" + elapsedT, 480, 250);
g.drawString("R", 364, 300);
}
g.dispose();
repaint();
}
#Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_W){
}
if (code == KeyEvent.VK_A){
player.setdirectionX(-1);
}
if (code == KeyEvent.VK_S){
}
if (code == KeyEvent.VK_D){
player.setdirectionX(1);
}
if (code == KeyEvent.VK_R){
}
}
public void setGameOver(boolean flag){
isGameOver = flag;
}
#Override
public void keyReleased(KeyEvent e){
player.setdirectionX(0);
player.setdirectionY(0);
}
#Override
public void keyTyped(KeyEvent e) {
}
public Stage getStage(){
return stage;
}
public EnemyManager getEnemyManager(){
return manager;
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setTitle("Dodge the bloody rectangles");
frame.add(new Main());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800,600));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
public void restart() {
stage = new Stage();
player = new Player(this, 200, 500);
manager = new EnemyManager(this, 10);
isGameOver = false;
}
...
#Override
public void keyPressed(KeyEvent e) {
...
if (code == KeyEvent.VK_R){
restart();
}
}

Java GUI drawing an image on mouse event

I'm making a program where hovering over a grid will place an image into the cell hovered over. Currently I have it working so that only a color is filled in, but I have no idea how to make it so an image is drawn in instead. Here's my program:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class Test
{
Image img=Toolkit.getDefaultToolkit().getImage("img.gif");
public static void main(String[]args)
{
new Test();
}
public Test()
{
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PixelGrid());
frame.setSize(new Dimension(364,357));
frame.setVisible(true);
}
public class PixelGrid extends JPanel
{
private List<Shape>grid,square;
public PixelGrid()
{
grid=new ArrayList<>();
square=new ArrayList<>();
addMouseMotionListener(new MouseAdapter()
{
public void mouseMoved(MouseEvent e)
{
for(Shape shape:grid)
{
if(shape.contains(e.getPoint()))
square.add(shape);
}
repaint();
}
}
);
for(int row=0;row<5;row++)
{
for(int col=0;col<5;col++)
grid.add(new Rectangle(col*25+112,row*25+50,25,25));
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawLine(112,50,237,50);
g.drawLine(112,75,237,75);
g.drawLine(112,100,237,100);
g.drawLine(112,125,237,125);
g.drawLine(112,150,237,150);
g.drawLine(112,175,237,175);
g.drawLine(112,50,112,175);
g.drawLine(137,50,137,175);
g.drawLine(162,50,162,175);
g.drawLine(187,50,187,175);
g.drawLine(212,50,212,175);
g.drawLine(237,50,237,175);
Graphics2D g2=(Graphics2D)g;
for(Shape cell:square)
g2.fill(cell);
}
}
}
I want to make the image "img" become the "pixel" that is filled in. However, I'm confused on how to do this as I don't think I can use for-each loops and Graphics2D. If anyone can help, thanks so much!
Define ImageHolder class which has 2 fields shape and image.
class ImageHolder {
Shape shape;
Image img;
public void paint(Graphics2D g2) {
if (img!=null) {
g2.drawImage(img);
}
else {
g2.fill(shape);
}
}
}
Your grid should be List. On init all the ImageHolders have squares and null images. On click image is assigned to the clicked holder.
In the paintComponent() you just call the holder's paint() method
take a look at this one.
all you need to do is repalce your g2.fill(cell); with g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);
read drawimage api here
for (Shape cell : square) {
//g2.fill(cell);
g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);
System.out.println(cell.getBounds().x);
}
however use Dimention list or 2D list instead Shape in this case
complete code
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class Test {
Image img = Toolkit.getDefaultToolkit().getImage("img.gif");
public static void main(String[] args) {
new Test();
}
public Test() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PixelGrid());
frame.setSize(new Dimension(364, 357));
frame.setVisible(true);
}
public class PixelGrid extends JPanel {
int x=0;
int y=0;
private List<Shape> grid, square;
public PixelGrid() {
grid = new ArrayList<>();
square = new ArrayList<>();
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
for (Shape shape : grid) {
if (shape.contains(e.getPoint())) {
square.add(shape);
}
}
repaint();
}
}
);
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
grid.add(new Rectangle(col * 25 + 112, row * 25 + 50, 25, 25));
}
}
}
public void paintComponent(Graphics g) {
System.out.println(x);
g.drawLine(112, 50, 237, 50);
g.drawLine(112, 75, 237, 75);
g.drawLine(112, 100, 237, 100);
g.drawLine(112, 125, 237, 125);
g.drawLine(112, 150, 237, 150);
g.drawLine(112, 175, 237, 175);
g.drawLine(112, 50, 112, 175);
g.drawLine(137, 50, 137, 175);
g.drawLine(162, 50, 162, 175);
g.drawLine(187, 50, 187, 175);
g.drawLine(212, 50, 212, 175);
g.drawLine(237, 50, 237, 175);
Graphics2D g2 = (Graphics2D) g;
//g2.drawImage(img,x,y,null);
for (Shape cell : square) {
//g2.fill(cell);
g2.drawImage(img,cell.getBounds().x,cell.getBounds().y,null);
System.out.println(cell.getBounds().x);
}
}
}
}
output>>

Drawing a shape by clicking a JButton

I am currently trying to draw a figure by clicking a button. My problems occurs when I click the button and it does not show up in the panel, but I know it's drawing because it runs through the loop.
The drawing will occur when I request the panel to draw it inside the constructor, but not inside the button, inside the constructor
If i put the code in the method "stuff()" inside the constructor it will draw everything just fine.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame{
public static void main(String[]args){
MainFrame f = new MainFrame();
}
public JFrame frame = new JFrame();
public JPanel panel = new JPanel(new BorderLayout());
public MainFrame(){
JButton button1 = new JButton("Shweet Button");
button1.setBounds(185, 10, 130, 20);
frame.setBounds(1680/4,1050/4,500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
panel.setBackground(Color.black);
frame.setVisible(true);
frame.getContentPane().setLayout(null);
frame.add(button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stuff();
}
});
}
public void stuff(){
for(int i = 0;i<1000;i++){
panel.add(new paintComponent());
panel.setBackground(Color.black);
frame.repaint();
try {
Thread.sleep(80);
} catch (InterruptedException e1){e1.printStackTrace();}
}
}
static class paintComponent extends JComponent{
public int options;
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
if(options == 0){
options = 1;
g2.drawOval(50, (JFrame.HEIGHT/2)+100, 50, 50);
g2.drawOval(60, (JFrame.HEIGHT/2)+110, 8, 8);
g2.fillOval(60, (JFrame.HEIGHT/2)+110, 8, 8);
g2.drawOval(80, (JFrame.HEIGHT/2)+110, 8, 8);
g2.fillOval(80, (JFrame.HEIGHT/2)+110, 8, 8);
g2.drawArc(65, (JFrame.HEIGHT/2)+130, 100, 0, 140, 30);
g2.drawLine(75, (JFrame.HEIGHT/2)+150, 75, (JFrame.HEIGHT/2)+220);
g2.drawLine(75, (JFrame.HEIGHT/2)+180, 100, (JFrame.HEIGHT/2)+160);
g2.drawLine(75, (JFrame.HEIGHT/2)+180, 65, (JFrame.HEIGHT/2)+210);
g2.drawLine(75, (JFrame.HEIGHT/2)+220, 50, (JFrame.HEIGHT/2)+260);
g2.drawLine(75, (JFrame.HEIGHT/2)+220, 100, (JFrame.HEIGHT/2)+260);
}else if(options == 1){
options = 0;
g2.drawOval(50, (JFrame.HEIGHT/2)+100, 50, 50);
g2.drawOval(60, (JFrame.HEIGHT/2)+110, 8, 8);
g2.fillOval(60, (JFrame.HEIGHT/2)+110, 8, 8);
g2.drawOval(80, (JFrame.HEIGHT/2)+110, 8, 8);
g2.fillOval(80, (JFrame.HEIGHT/2)+110, 8, 8);
g2.drawArc(65, (JFrame.HEIGHT/2)+130, 100, 0, 140, 30);
g2.drawLine(75, (JFrame.HEIGHT/2)+150, 75, (JFrame.HEIGHT/2)+220);
g2.drawLine(75, (JFrame.HEIGHT/2)+180, 100, (JFrame.HEIGHT/2)+180);
g2.drawLine(75, (JFrame.HEIGHT/2)+180, 65, (JFrame.HEIGHT/2)+210);
g2.drawLine(75, (JFrame.HEIGHT/2)+220, 50, (JFrame.HEIGHT/2)+260);
g2.drawLine(75, (JFrame.HEIGHT/2)+220, 100, (JFrame.HEIGHT/2)+260);
}
}
}
}
You are blocking the Event Dispatching Thread, which is responsible for, amongst other things, processing paint requests.
You should never do anything like this...
for(int i = 0;i<1000;i++){
panel.add(new paintComponent());
panel.setBackground(Color.black);
frame.repaint();
try {
Thread.sleep(80);
} catch (InterruptedException e1){e1.printStackTrace();}
}
Within the EDT. Apart from the fact you are adding multiple new components to your UI every 80 milliseconds, you are also blocking the thread that is responsible for updating your screen...
Check out Concurrency in Swing for more details.
Animation of this type should be handled by a javax.swing.Timer.
Custom painting should be performed in the paintComponent method, as a general rule. You should also be calling super.paintXxx first as a matter of course. There's a lot of working going in the background that needs to be taken care of, especially if the component is transparent.
Check out Performing Custom Painting and Painting in AWT and Swing for more details.
Save your sanity and learn how to use layout managers, they will make your life easier in the long run.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MainFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
MainFrame f = new MainFrame();
}
});
}
public JFrame frame = new JFrame();
public JPanel panel = new JPanel(new BorderLayout());
private WavePane waver;
public MainFrame() {
waver = new WavePane();
JButton button1 = new JButton("Shweet Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setBackground(Color.black);
frame.add(button1, BorderLayout.SOUTH);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stuff();
}
});
panel.add(waver);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void stuff() {
// for (int i = 0; i < 1000; i++) {
// panel.add(new paintComponent());
// panel.setBackground(Color.black);
// frame.repaint();
// try {
// Thread.sleep(80);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
// }
waver.walk(!waver.isWaving());
}
public class WavePane extends JComponent {
private int options;
private Timer timer;
public WavePane() {
setOpaque(false);
timer = new Timer(80, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("tick");
options++;
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public void walk(boolean walk) {
if (walk) {
timer.start();
} else {
timer.stop();
}
}
public boolean isWaving() {
return timer.isRunning();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.white);
int height = getHeight();
if (options % 2 == 0) {
g2.drawOval(50, 100, 50, 50);
g2.drawOval(60, 110, 8, 8);
g2.fillOval(60, 110, 8, 8);
g2.drawOval(80, 110, 8, 8);
g2.fillOval(80, 110, 8, 8);
g2.drawArc(65, 130, 100, 0, 140, 30);
g2.drawLine(75, 150, 75, 220);
g2.drawLine(75, 180, 100, 160);
g2.drawLine(75, 180, 65, 210);
g2.drawLine(75, 220, 50, 260);
g2.drawLine(75, 220, 100, 260);
} else {
g2.drawOval(50, 100, 50, 50);
g2.drawOval(60, 110, 8, 8);
g2.fillOval(60, 110, 8, 8);
g2.drawOval(80, 110, 8, 8);
g2.fillOval(80, 110, 8, 8);
g2.drawArc(65, 130, 100, 0, 140, 30);
g2.drawLine(75, 150, 75, 220);
g2.drawLine(75, 180, 100, 180);
g2.drawLine(75, 180, 65, 210);
g2.drawLine(75, 220, 50, 260);
g2.drawLine(75, 220, 100, 260);
}
}
}
}
You shouldn't be using JFrame.HEIGHT this actually has nothing to do with the frames height, but is part of the ImageObserver support... or any type of magic number for that matter

Categories