Related
I changed a program (see below) I nabbed and have managed to get it to do some of what I want. I need a number of rows/bands/stripes of 3 to blink in sequence. If you could imagine three sets of three vertical bars/bands and each bar numbered 1-3. I want to get each band/bar numbered 1 of each group to blink. So all bands numbered 1 blinks for a finite time, then bands numbered 2, then 3 then repeat. So when looking at it it looks like vertical stripes blinking 1,2,3,1,2,3 etc.
What's set out below and what I have described as boxes below refers to the bands I am talking about
class BelishN {
private Timer timer;
public class Drawing extends JPanel {
private int x = 125;// Not required - it was for the ball!
private int y = 80;
private boolean changeColors = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//creating the shapes
Rectangle box1 = new Rectangle(1, 1, 10, 770);
//Rectangle box2 = new Rectangle(165, 225, 20, 45);
Rectangle box3 = new Rectangle(10, 1, 10, 770);
//Rectangle box4 = new Rectangle(165, 315, 20, 45);
Rectangle box5 = new Rectangle(20, 1, 10, 770);
// Rectangle box6 = new Rectangle(165, 405, 20, 45);
//drawing the shapes
//Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
//g2.draw(ball);
g2.draw(box1);
//g2.draw(box2);
//g2.draw(box3);
//g2.draw(box4);
//g2.draw(box5);
//g2.draw(box6);
//coloring the shapes
g2.setColor(Color.BLACK);
g2.fill(box1);
g2.fill(box3);
g2.fill(box5);
g2.setColor(Color.ORANGE);
//g2.fill(ball);
changeColors = !changeColors;
if (changeColors) {
g2.setColor(Color.white);
//g2.fill(new Ellipse2D.Double(x, y, 100, 100));
g2.fill(box1 = new Rectangle(1, 1, 10, 770));//3 vertical bands are flashing together
g2.fill(box3 = new Rectangle(10, 1, 10, 770));
g2.fill(box5 = new Rectangle(20, 1, 10, 770));
}
}
public void changeColors() {
changeColors = true;
repaint();
}
}
public BelishN() {
//Creation of frame
JFrame frame = new JFrame();
frame.setSize(800, 770); //Screen size
frame.setTitle("Belisha Beacon");
frame.setLayout(new BorderLayout(0, 0));
final Drawing shapes = new Drawing();
timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
shapes.repaint();
}
});
JButton jbtFlash = new JButton("Flash");
jbtFlash.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
timer.start();
}
});
final JButton jbtSteady = new JButton("Steady");
jbtSteady.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();
}
});
//Positioning
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
controlPanel.add(jbtFlash);
controlPanel.add(jbtSteady);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.add(shapes);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// in last line here, start the timer:
timer.start();
}
public static void main(String[] args) {
new BelishN();
}
}
Here what I want is that when the Buttons are clicked the shapes should appear immediately, but the shapes does not appear unless the JFrame is minimized and restored.
package tutorial;
public class Tutorial extends JPanel{
JButton b1,b2,b3,b4,b5;
boolean nodeA, nodeB,communicate, key, acknowledge = false;
public Tutorial(){
b1 = new JButton("Node A");
add(b1);
b2 = new JButton("Node B");
add(b2);
b3 = new JButton("Communicate");
add(b3);
b4 = new JButton("send key");
add(b4);
b5 = new JButton("Request B");
add(b5);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(300, 100,100, 50);
g.setColor(Color.red);
g.drawString("KDC/KMC",320,130);
b1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
nodeA = true;
}
});
if(nodeA == true){
g.setColor(Color.green);
g.fillOval(100, 300, 70, 70);
g.setColor(Color.red);
g.drawString("Node A", 113, 340);
g.drawString("Node A added",150,220);
g.drawLine(150, 300, 300, 150);
}
b2.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
nodeB = true;
}
});
if(nodeB == true){
g.setColor(Color.green);
g.fillOval(530, 300, 70, 70);
g.setColor(Color.red);
g.drawString("Node B",545,340);
g.drawString("Node B added",473,220);
g.drawLine(550, 300, 400, 150);
}
b3.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
if(nodeA == true && nodeB == true)
communicate = true;
}
});
if(communicate == true){
g.drawString("A requests for B's Session Key", 230,260);
g.drawLine(165, 310, 325, 150);
}
b4.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
if(communicate == true)
key = true;
}
});
if(key == true){
g.drawString("A's key is 4",210,175);
g.drawString("B's key is 5",430,175);
}
b5.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
if(key == true)
acknowledge = true;
}
});
if(acknowledge == true){
g.drawLine(170,325,530,325);
g.drawString("A sends part of session key to B", 260, 320);
g.drawLine(170,350,530,350);
g.drawString("if sessiion key match then B sent Acknowledgement", 215, 365);
}}
public static void main(String[] args) {
Tutorial t = new Tutorial();
JFrame jf = new JFrame("Assignment");
jf.setSize(1200,900);
jf.add(t);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Here it looks like when clicked but not minimized enter image description here
and here it looks like when JFrame is minimized and restoredenter image description here
I assume you do something to add that green node later? If you modify classes directly without them being Swing classes, I think you manually have to issue a repaint() on your Swing class/container or Swing doesn't know that the components needs to be updated on the screen (Swing classes do this automatically when they are changed, so you don't have to do it when you call add() for example). – markspace
Related: Repaint() vs. Revalidate() stackoverflow.com/questions/1097366/… Do some searches too to understand how Swing's repaint system works (lots of tutorials). – markspace
This question already has answers here:
Show an animated BG in Swing
(3 answers)
Closed 6 years ago.
im making a mathematical game and want to use a gif as a background. Its dimensions are 1100*800
I searched many posts how to add a GIF as background, but with no success. Any suggestions for a easy method (if using other components -JPanel,...; could you please show how?)
So far, this is my code of the JFrame:
public class Game extends JFrame implements ActionListener {
private JButton play, endG, tutorial, login, easy, medium, hard, next, checkAnswer;
private JTextArea answer;
int total, goodAnswer = 0;
public Game(String heading) {
super(heading);
this.setSize(1100, 800);
this.setLayout(null);
firstScreen();
setResizable(false);
}
public void firstScreen() {
getContentPane().removeAll();
play = new JButton();
play.setBounds(373, 350, 354, 80);
play.setIcon(new ImageIcon("entrancePlayButton.png"));
play.addActionListener(this);
play.setOpaque(false);
play.setContentAreaFilled(false);
add(play);
tutorial = new JButton("Tutorial");
tutorial.setBounds(345, 520, 150, 50);
tutorial.setFont(new Font("Arial", Font.PLAIN, 20));
tutorial.addActionListener(this);
tutorial.setOpaque(false);
tutorial.setContentAreaFilled(false);
add(tutorial);
endG = new JButton("End Game");
endG.setBounds(605, 520, 150, 50);
endG.setFont(new Font("Arial", Font.PLAIN, 20));
endG.addActionListener(this);
endG.setOpaque(false);
endG.setContentAreaFilled(false);
add(endG);
revalidate();
repaint();
}
public void difficultyScreen() {
getContentPane().removeAll();
easy = new JButton("Easy");
easy.setBounds(450, 310, 200, 80);
easy.setFont(new Font("Arial", Font.PLAIN, 30));
easy.addActionListener(this);
easy.setOpaque(false);
easy.setContentAreaFilled(false);
add(easy);
medium = new JButton("Medium");
medium.setBounds(450, 440, 200, 80);
medium.setFont(new Font("Arial", Font.PLAIN, 30));
medium.addActionListener(this);
medium.setOpaque(false);
medium.setContentAreaFilled(false);
add(medium);
hard = new JButton("Hard");
hard.setBounds(450, 570, 200, 80);
hard.setFont(new Font("Arial", Font.PLAIN, 30));
hard.addActionListener(this);
hard.setOpaque(false);
hard.setContentAreaFilled(false);
add(hard);
endG = new JButton("Exit");
endG.setBounds(1000, 700, 60, 30);
endG.setFont(new Font("Arial", Font.PLAIN, 15));
endG.addActionListener(this);
endG.setOpaque(false);
endG.setContentAreaFilled(false);
add(endG);
revalidate();
repaint();
}
public void playGameScreen() {
getContentPane().removeAll();
revalidate();
repaint();
}
public void tutorialScreen() {
getContentPane().removeAll();
revalidate();
repaint();
}
private static double stringToDouble(String number) {
double num = Double.parseDouble(number);
return num;
}
public static void main() {
Game areaGame = new Game("Area Game");
areaGame.setVisible(true);
}
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource() == play) {
difficultyScreen();
}
if (actionEvent.getSource() == tutorial) {
tutorialScreen();
}
if (actionEvent.getSource() == endG) {
int reply = JOptionPane.showConfirmDialog(null, "You are about to exit the game, are you sure?", "Exit game", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
}
There are two ways to do this:
you can override the paintComponent() method of your JPanel.
like this:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(yourImage, 0, 0, this);
}
or you can use a JLabel by loading the image as an ImageIcon and then displaying it in a JLabel.
import java.awt.event.ActionEvent; //this is my button class
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Dimension;
public class Buttons extends JFrame{
private JPanel panel1, panel2;
private JButton button1, button2, button3;
private JMenuBar menuBar;
public Buttons()
{
createPanel();
addPanel();
}
private void createPanel()
{
setLocationRelativeTo(null);
panel1 = new JPanel();
panel1.setBackground(Color.cyan);
button1 = new JButton("Start");
button1.addActionListener(new addButtonListener());
button1.setBounds(50, 90, 190, 30);
button2 = new JButton("Instructions");
button2.setBounds(70, 130, 160, 30);
panel2 = new JPanel();
//button3 = new JButton("Test");
//panel2.setBackground(Color.orange);
//button3.setBounds(50, 50, 90, 30);
}
private void addPanel()
{
panel1.add(button1);
panel1.add(button2);
panel2.add(button3);
add(panel1);
}
class addButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
getContentPane().removeAll();
getContentPane().add(panel2);
repaint();
printAll(getGraphics());
}
}
public static void main(String[]args)
{
JMenuItem exitAction = new JMenuItem("Exit");
JMenuBar menuBar = new JMenuBar();
JMenu save = new JMenu("Save");
JMenu file = new JMenu("File");
JMenu credit = new JMenu("Credit");
file.add(exitAction);
menuBar.add(file);
menuBar.add(save);
menuBar.add(credit);
exitAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
Buttons frame = new Buttons();
frame.setTitle("Bikini Bottom Marathon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,500);
frame.setVisible(true);
frame.setJMenuBar(menuBar);
}
}
import javax.swing.*;
//this is my graphics class to create a board. the images are imported from saved
pictures in my class file. I'm trying to add the output from this code (the board)
onto the buttons class
import java.applet.*;
import java.awt.*;
import javax.imageio.*;
import java.net.*;
import java.io.*;
import java.awt.image.*;
import java.applet.*;
public class compscigame extends Applet
{
public void paint (Graphics page)
{
Image pineapple = getImage(getCodeBase(), "pineapple.jpg");
page.drawImage (pineapple, 50, 500, 50, 50, this);
Image raygun = getImage(getCodeBase(), "Raygun.jpg");
page.drawImage (raygun, 300, 350, 50, 50, this);
Image karen = getImage(getCodeBase(), "Karen.jpg");
page.drawImage(karen, 100, 50, 50, 50, this);
Image karensmad = getImage(getCodeBase(), "karensmad.jpg");
page.drawImage(karensmad, 150, 400, 50, 50, this);
Image rocketboots = getImage(getCodeBase(), "rocketboots.jpg");
page.drawImage(rocketboots, 200, 300, 50, 50, this);
Image emptybeaker = getImage(getCodeBase(), "emptybeaker.jpg");
page.drawImage(emptybeaker, 350, 150, 50, 50, this);
Image happykaren = getImage(getCodeBase(), "happykaren.jpg");
page.drawImage(happykaren, 100, 50, 50, 50, this);
Image raygunnotshooting = getImage(getCodeBase(), "raygunnotshooting.jpg");
page.drawImage (raygunnotshooting, 450, 200, 50, 50, this);
Image notfirerocket = getImage(getCodeBase(), "notfirerocket.jpg");
page.drawImage (notfirerocket, 450, 450, 50, 50, this);
Image firerocket = getImage(getCodeBase(), "firerocket.jpg");
page.drawImage (firerocket, 500, 200, 50, 50, this);
Image fullbeaker = getImage(getCodeBase(), "fullbeaker.jpg");
page.drawImage (fullbeaker, 250, 250, 50, 50, this);
Image boots = getImage(getCodeBase(), "boots.jpg");
page.drawImage (boots, 300, 500, 50, 50, this);
Image krustykrab = getImage(getCodeBase(), "krustykrab.jpg");
page.drawImage(krustykrab, 50, 50, 50, 50, this);
Board board = new Board(page, new Color (255,0,100));
setBackground(Color.YELLOW);
}
class Board
{
private Graphics g;
private Color col;
private Square [][] squares;
public Board (Graphics g, Color col)
{
this.col = col;
this.g = g;
Color temp;
squares = new Square[10][10];
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
int num;
if (i + j % 2 == 0)
temp = col;
else
temp = new Color(255, 0 , 255);
Square t;
t = new Square (g, temp, i, j);
if (j % 2 != 0)
{
num = 10 - i + 1 + 10 * (j - 1);
}
else
{
num = i + 10 * (j - 1);
}
t.setVal(num);
t.drawSquare();
squares[i - 1][j - 1] = t;
}
}
}
}
class Square
{
private Color col;
private int x;
private int y;
private Graphics g;
private int val;
public Square (Graphics g, Color col, int x, int y)
{
this.col = col;
this.g = g;
this.x = x;
this.y = y;
val = 0;
}
public void setVal(int num)
{
val = num;
}
public void drawSquare()
{
g.setColor(col);
g.drawRect(50 + (10 - x) * 50, 50 + (10 - y) * 50, 50, 50);
String str = "" + val;
g.drawString(str, 50 + (10 - x) * 50 + 5, 50 + (10 - y) * 50 + 10);
}
}
I just want the board to be in another panel. As you can see I used ActionListener to create the action my buttons will do. When you click Start it takes you to the button that says "test." I want the graphic from the Board to show on this second panel, (panel2)
}
Make a custom inner class that extends JPanel and then override the onPaintComponent method. Draw the Board there. Create a new BoardPanel instead of a JPanel.
Put your paint code in a seperate class (JPanel)
public void paint (Graphics page)
{
...
}
There is no need for the applet to have a dependency on any of this code.
Alright, I figured out everything that I got but now I am really stuck. Every time you choose a different shape the previously selected one disappears. How do I make it so they don't disappear and stay on the screen until you exit?
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class ShapeStamper extends JFrame{
Random rand = new Random();
public int x;
public int y;
private JPanel panel1, panel2;
private JButton button1, button2, button3, button4;
private int option = 0;
public ShapeStamper(){
super("Shape Stamper!");
panel1 = new JPanel();
button1 = new JButton("Circle");
button2 = new JButton("Square");
button3 = new JButton("Rectangle");
button4 = new JButton("Oval");
button1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 1;
}
}
);
button2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 2;
}
}
);
button3.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 3;
}
}
);
button4.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 4;
}
}
);
panel2 = new JPanel();
panel2.setBackground(Color.WHITE);
MouseHandler mouse = new MouseHandler();
setVisible(true);
addMouseListener(mouse);
addMouseMotionListener(mouse);
add(panel2);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
add(panel1, BorderLayout.SOUTH);
setSize(500,500);
setVisible(true);
}
private class MouseHandler extends MouseAdapter implements MouseMotionListener{
#Override
public void mousePressed(MouseEvent e){
x = e.getX();
y = e.getY();
repaint();
}
}
public void paint(Graphics g){
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
if(option == 0){
g.setFont(new Font("Serif", Font.BOLD, 32));
g.drawString("Shape Stamper!", 150, 220);
g.setFont(new Font("Serif", Font.ITALIC, 16));
g.drawString("Programmed by: Chris", 150, 230);
}
if(option == 1){
Color randColor1 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor1);
g2d.drawOval(50, 50, 100, 100);
}
if(option == 2){
Color randColor2 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor2);
g2d.drawRect(50, 50, 100, 100);
}
if(option == 3){
Color randColor3 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor3);
g2d.draw(new Rectangle2D.Double(75,50,150,100));
}
if(option == 4){
Color randColor4 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor4);
g2d.draw(new Ellipse2D.Double(50, 25, 100, 50));
}
}
public static void main(String[] args) {
ShapeStamper application = new ShapeStamper();
application.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
You should not be overriding paint of a top level container and then trying to painting over the top of the components you have already added.
The basic problem you will encounter is, the paint system is clever enough that it may not ever call paint of the frame, but simply update the child components directly instead.
Instead, create yourself a custom component, extending from something like JPanel and override it's paintComponent method and perform your custom painting there. Then add this component to your frame.
You will also find the the paint updates are cleaner and won't flicker when updated.
You should also make sure you are calling repaint on this custom component when ever you change one it's options to ensure that changes are painted back to the component
Take a look at Performing Custom Painting for more details.
Also, just to be clear, you should not be calling super.paintComponents from paint (or in fact anywhere except for when you override paintComponents...which there really should be a need to do...)