Shapes are not drawn in real time - java

So, if I try to move the shape, it actually moves, but there is no motion animation. In order for the moved shape to be drawn, the application window must be minimized or maximized.
Here is the PentaminoShape class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class PentominoShape extends JFrame implements MouseListener, MouseMotionListener {
JPanel shapePane;
Container contentPane;
private Polygon currPolygon;
private int x, y;
ArrayList<Polygon> polygons = new ArrayList<Polygon>();
JFrame frame;
public PentominoShape(JFrame frame){
this.frame = frame;
initShape();
}
private void initShape() {
Polygon fig1 = new Polygon(new int[]{10, 50, 50, 10}, new int[]{10, 10, 200, 200}, 4);
Polygon fig2 = new Polygon(new int[]{130, 210, 210, 170, 170, 130, 130, 90, 90, 130}, new int[]{80, 80, 120, 120, 200, 200, 160, 160, 120, 120}, 10);
Polygon fig3 = new Polygon(new int[]{290, 330, 330, 250, 250, 290}, new int[]{50, 50, 200, 200, 160, 160}, 6);
Polygon fig4 = new Polygon(new int[]{10, 90, 90, 50, 50, 10}, new int[]{280, 280, 400, 400, 360, 360}, 6);
Polygon fig5 = new Polygon(new int[]{170, 210, 210, 170, 170, 130, 130, 170}, new int[]{240, 240, 360, 360, 400, 400, 320, 320}, 8);
Polygon fig6 = new Polygon(new int[]{250, 370, 370, 330, 330, 290, 290, 250}, new int[]{280, 280, 320, 320, 400, 400, 320, 320}, 8);
Polygon fig7 = new Polygon(new int[]{10, 50, 50, 90, 90, 130, 130, 10}, new int[]{480, 480, 520, 520, 480, 480, 560, 560}, 8);
Polygon fig8 = new Polygon(new int[]{170, 250, 250, 290, 290, 170}, new int[]{520, 520, 440, 440, 560, 560}, 6);
Polygon fig9 = new Polygon(new int[]{330, 370, 370, 410, 410, 450, 450, 410, 410, 330}, new int[]{520, 520, 480, 480, 440, 440, 520, 520, 560, 560}, 10);
Polygon fig10 = new Polygon(new int[]{10, 50, 50, 90, 90, 130, 130, 90, 90, 50, 50, 10}, new int[]{680, 680, 640, 640, 680, 680, 720, 720, 760, 760, 720, 720}, 12);
Polygon fig11 = new Polygon(new int[]{170, 210, 210, 250, 250, 210, 210, 170}, new int[]{640, 640, 600, 600, 760, 760, 680, 680}, 8);
Polygon fig12 = new Polygon(new int[]{330, 410, 410, 370, 370, 290, 290, 330}, new int[]{640, 640, 680, 680, 760, 760, 720, 720}, 8);
polygons.add(fig1);
polygons.add(fig2);
polygons.add(fig3);
polygons.add(fig4);
polygons.add(fig5);
polygons.add(fig6);
polygons.add(fig7);
polygons.add(fig8);
polygons.add(fig9);
polygons.add(fig10);
polygons.add(fig11);
polygons.add(fig12);
Color[] c = new Color[12];
c[0] = new Color(25, 165, 25);
c[1] = new Color(255, 165, 25);
c[2] = new Color(255, 50, 50);
c[3] = new Color(150, 45, 90);
c[4] = new Color(25, 165, 150);
c[5] = new Color(25, 165, 255);
c[6] = new Color(255, 40, 190);
c[7] = new Color(180, 90, 60);
c[8] = new Color(90, 80, 70);
c[9] = new Color(70, 80, 90);
c[10] = new Color(150, 30, 20);
c[11] = new Color(80, 80, 80);
shapePane = new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(c[0]); g2.fill(fig1);
g2.setColor(c[1]); g2.fill(fig2);
g2.setColor(c[2]); g2.fill(fig3);
g2.setColor(c[3]); g2.fill(fig4);
g2.setColor(c[4]); g2.fill(fig5);
g2.setColor(c[5]); g2.fill(fig6);
g2.setColor(c[6]); g2.fill(fig7);
g2.setColor(c[7]); g2.fill(fig8);
g2.setColor(c[8]); g2.fill(fig9);
g2.setColor(c[9]); g2.fill(fig10);
g2.setColor(c[10]); g2.fill(fig11);
g2.setColor(c[11]); g2.fill(fig12);
}
};
/*contentPane = this.getContentPane();
contentPane.add(shapePane);
this.pack();*/
frame.add(shapePane);
shapePane.addMouseListener(this);
shapePane.addMouseMotionListener(this);
/*shapePane.addMouseListener(this);
shapePane.addMouseMotionListener(this);*/
}
public void mousePressed(MouseEvent e) {
for(Polygon polygon: polygons) {
if (polygon.contains(e.getPoint())) {
System.out.println("Pressed");
currPolygon = polygon;
x = e.getX();
y = e.getY();
}
}
}
public void mouseDragged(MouseEvent e) {
try {
if (currPolygon.contains(x, y)) {
System.out.println("Dragged");
int dx = e.getX() - x;
int dy = e.getY() - y;
currPolygon.translate(dx, dy);
x += dx;
y += dy;
repaint();
}
}catch (NullPointerException ex){
}
}
public void mouseReleased(MouseEvent e){
currPolygon = null;
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
}
And the main Pentamino class:
import javax.swing.*;
public class Pentomino extends JFrame {
JFrame frame;
PentominoShape shape;
PentominoPanel panel;
public Pentomino(){
initUI();
}
private void initUI(){
frame = new JFrame("Пентамино");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(1500, 900);
setResizable(false);
shape = new PentominoShape(frame);
panel = new PentominoPanel(frame);
/*frame.add(shape);
frame.add(panel);*/
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
Pentomino game = new Pentomino();
}
}
I'm new to Java and I don't understand how to solve this problem. Tried searching the internet for a similar problem but couldn't find anything.

Your bug is here:
public void mouseDragged(MouseEvent e) {
try {
if (currPolygon.contains(x, y)) {
System.out.println("Dragged");
int dx = e.getX() - x;
int dy = e.getY() - y;
currPolygon.translate(dx, dy);
x += dx;
y += dy;
repaint(); // ***** here ****
}
}catch (NullPointerException ex){
}
}
You're calling repaint() on the enclosing class, a JFrame, one that is never displayed, and this will have not have the effect desired.
In fact, ask yourself, why this...
public class PentominoShape extends JFrame // ...
Why is PentominoShape extending JFrame at all, when it isn't behaving as a JFrame, when it shouldn't be behaving as a JFrame?
Instead, call repaint on the JPanel that holds the shapes, the shapePane JPanel, and yes, get rid of catching the NullPointerException. That should never be in your code:
public void mouseDragged(MouseEvent e) {
if (currPolygon == null) {
return;
}
if (currPolygon.contains(x, y)) {
System.out.println("Dragged");
int dx = e.getX() - x;
int dy = e.getY() - y;
currPolygon.translate(dx, dy);
x += dx;
y += dy;
shapePane.repaint(); // now we're repainting the correct JPanel!
}
}
Side note: I'd clean things up a bit including
Not having any class extend JFrame if possible
Create my own polygon type of class:
public class PentominoShape2 {
private Polygon polygon;
private Color color;
public PentominoShape2(Polygon polygon, Color color) {
this.polygon = polygon;
this.color = color;
}
public void draw(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(color);
g2.fill(polygon);
}
public Polygon getPolygon() {
return polygon;
}
public Color getColor() {
return color;
}
public boolean contains(Point p) {
return polygon.contains(p);
}
}
and then in the drawing JPanel
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (PentominoShape2 poly : polys) {
poly.draw(g);
}
}
same for the mouse listener

Related

How do I change these panels using CardLayout?

So, I am going to have more of these panels but I don't know how to make these panels switch when the user will press a button like start or next.
//IngestionOfDigestion.java teaches the user about the digestive system through interactive components
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class IngestionOfDigestion extends JFrame {
public static void main(String[] args) {
IngestionOfDigestion i = new IngestionOfDigestion();
}
public IngestionOfDigestion() {//frame header
//make the frame
super("Ingestion Of Digestion");
setSize(1900, 1100);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(10, 50);
setResizable(true);
layout t = new layout();
add(t);
setContentPane(t);
setVisible(true);
}
}
class layout extends JPanel {
public Mouth m;
public CardLayout cardlay = new CardLayout();
Title title = new Title();
public layout() {
m = new Mouth();
cardlay.show();
setLayout(cardlay);
add(m, "Mouth");
add(title, "Title");
}
}
class Title extends JPanel implements ActionListener {//make title panel
private JButton c;//jbutton to start the game
private boolean startpressed;//button pressed variable
//JPanel card2;
//JPanel card3;
public Title() {//title method header
//cardstack = new JPanel(cardlay);
c = new JButton("Press to start playing");//make jbutton saying start the game
c.addActionListener(this);
add(c);
startpressed = false;
}
public void paintComponent(Graphics g) { //paint component
super.paintComponent(g);//super.paintComponent
Font title = new Font("Serif", Font.BOLD, 90);//make font
g.setFont(title);
g.drawString("Ingestion Of Digestion", 160, 100);//draw the title
if (startpressed == true) {//if buttonpressed is true
repaint();
}
}
public void actionPerformed(ActionEvent e) {//ActionPerformed
if (e.getSource() == c) {
startpressed = true;//change boolean
cardlay.next();
repaint();
}
}
}
//if button is pressed
//end title panel
class Mouth extends JPanel implements ActionListener, ChangeListener {
private JButton chew;//make chew button
private JButton next;//next button
private JSlider saliva;//make saliva slider
private boolean nextpressed;//nextbuttonpressed variable
private boolean chewpressed;//buttonpressed variable
Mouth() {//mouth method
chew = new JButton("Press to chew the food");
chew.addActionListener(this);
add(chew);
chewpressed = false;
JSlider saliva = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
add(saliva);
next = new JButton("Press to go to the next step of digestion");
next.addActionListener(this);
add(next);
nextpressed = false;
}
public void paintComponent(Graphics g) {//paint component
super.paintComponent(g);//super.paintComponent
//draw the head
Color skin = new Color(255, 204, 133);
g.setColor(skin);
g.fillArc(810, 1000, 550, 550, 0, 180);
g.fillRect(1085, 600, 150, 550);
g.fillRect(700, 250, 535, 380);
int[] w = {700, 700, 660};
int[] t = {340, 420, 420};
g.fillPolygon(w, t, 3);
Color hair = new Color(100, 60, 0);
g.setColor(hair);
g.fillArc(700, 175, 535, 150, 0, 180);
g.setColor(Color.WHITE);
g.fillOval(730, 275, 45, 45);
g.setColor(Color.BLACK);
g.fillOval(752, 297, 15, 15);
g.setColor(Color.RED);
g.fillRect(700, 480, 285, 75);
g.fillRect(985, 497, 145, 45);
g.fillRect(1130, 497, 30, 590);
g.setColor(Color.WHITE);
g.fillRect(702, 480, 15, 20);
g.fillRect(719, 480, 15, 20);
g.fillRect(736, 480, 15, 20);
g.fillRect(753, 480, 24, 20);
g.fillRect(779, 480, 24, 20);
g.fillRect(805, 480, 24, 20);
g.fillRect(702, 535, 15, 20);
g.fillRect(719, 535, 15, 20);
g.fillRect(736, 535, 15, 20);
g.fillRect(753, 535, 24, 20);
g.fillRect(779, 535, 24, 20);
g.fillRect(805, 535, 24, 20);
if (chewpressed == true) {
Color skint = new Color(255, 204, 133);
g.setColor(skint);
g.fillArc(810, 1000, 550, 550, 0, 180);
g.fillRect(1085, 600, 150, 550);
g.fillRect(700, 250, 285, 380);
int[] wt = {700, 700, 660};
int[] tw = {340, 420, 420};
g.fillPolygon(w, t, 3);
Color hairt = new Color(100, 60, 0);
g.setColor(hairt);
g.fillArc(700, 175, 535, 150, 0, 180);
g.setColor(Color.WHITE);
g.fillOval(730, 275, 45, 45);
g.setColor(Color.BLACK);
g.fillOval(752, 297, 15, 15);
g.setColor(Color.RED);
g.fillRect(700, 480, 285, 42);
g.fillRect(985, 497, 145, 35);
g.fillRect(1130, 497, 30, 590);
g.setColor(Color.WHITE);
g.fillRect(702, 480, 15, 20);
g.fillRect(719, 480, 15, 20);
g.fillRect(736, 480, 15, 20);
g.fillRect(753, 480, 24, 20);
g.fillRect(779, 480, 24, 20);
g.fillRect(805, 480, 24, 20);
g.fillRect(702, 502, 15, 20);
g.fillRect(719, 502, 15, 20);
g.fillRect(736, 502, 15, 20);
g.fillRect(753, 502, 24, 20);
g.fillRect(779, 502, 24, 20);
g.fillRect(805, 502, 24, 20);
repaint();
}
}
public void actionPerformed(ActionEvent e) {//ActionPerformed
if (chew.getText().equals("Press to chew the food")) {//if "press my belly" pressed,
chewpressed = true;//change boolean
repaint();
if (next.getText().equals("Press to go to the next step of digestion")) {
nextpressed = true;
}
repaint();
}
}
public void stateChanged(ChangeEvent e) {
int location1 = saliva.getValue();
}
}
//draw the info
//if buttonpressed is true
//show head with teeth clenched
//else if slidermoved is true
//show saliva moving in mouth
//else if nextbuttonpressed is true
//go to next panel
//ActionPerformed
//if button is pressed
//buttonpressed is true
//else if slider is moved
//slidermoved is true
//else if next button is pressed
//nextbuttonpressed is true

mouse clicked event in Graphics2D in java

hello thanks in advance guys i need to find the X and y coordinates values by Mouse clicking on the draw panel of Graphics2D,and i need to get the string values of the coordinates randomly change the color of the coordinates.
package com.zetcode;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
class DrawPanel extends JPanel {
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(212, 212, 212));
g2d.drawRect(10, 15, 90, 60);
g2d.drawRect(130, 15, 90, 60);
g2d.drawRect(250, 15, 90, 60);
g2d.drawRect(10, 105, 90, 60);
g2d.drawRect(130, 105, 90, 60);
g2d.drawRect(250, 105, 90, 60);
g2d.drawRect(10, 195, 90, 60);
g2d.drawRect(130, 195, 90, 60);
g2d.drawRect(250, 195, 90, 60);
g2d.setColor(new Color(125, 167, 116));
g2d.fillRect(10, 15, 90, 60);
g2d.setColor(new Color(42, 179, 231));
g2d.fillRect(130, 15, 90, 60);
g2d.setColor(new Color(70, 67, 123));
g2d.fillRect(250, 15, 90, 60);
g2d.setColor(new Color(130, 100, 84));
g2d.fillRect(10, 105, 90, 60);
g2d.setColor(new Color(252, 211, 61));
g2d.fillRect(130, 105, 90, 60);
g2d.setColor(new Color(241, 98, 69));
g2d.fillRect(250, 105, 90, 60);
g2d.setColor(new Color(217, 146, 54));
g2d.fillRect(10, 195, 90, 60);
g2d.setColor(new Color(63, 121, 186));
g2d.fillRect(130, 195, 90, 60);
g2d.setColor(new Color(31, 21, 1));
g2d.fillRect(250, 195, 90, 60);
}
public void mouseClicked(MouseEvent me) {
int x1 = me.getX();
int y1 = me.getY();
System.out.println(x1);
JOptionPane.showMessageDialog(this, x1);
JOptionPane.showMessageDialog(this, y1);
//System.out.println(y1);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
public class RectanglesExample extends JFrame {
public RectanglesExample() {
initUI();
}
public final void initUI() {
DrawPanel dpnl = new DrawPanel();
add(dpnl);
setSize(360, 300);
setTitle("Rectangles");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
RectanglesExample ex = new RectanglesExample();
ex.setVisible(true);
}
});
}
}
Concerning the printing of the coordinates: Declare an inner class as follows
public class MyMouseListener extends MouseAdapter{
#Override
public void mouseClicked(MouseEvent e) {
int x1 = e.getX();
int y1 = e.getY();
System.out.println(x1);
System.out.println(y1);
repaint();
}
}
and add a constructor which register the MyMouseListener
public DrawPanel(){
this.addMouseListener(new MyMouseListener());
}
This will print you the coordinates every time you click on the panel.

How to execute an action when x and y location enters another location? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make a little racing game for fun. So far I have two rectangles that successfully move and I have a map setup for them to race through.
My map is also made up of rectangles. Now previously I made a mistake by not giving my two racers a specific objectified name. So they're just two locations that move. Now what i'm trying to do is, to make the rectangle walls actually be walls so they don't just go through them. I've heard I can cover up my mistakes if I make the walls like arrays(not sure how) so they don't go through them. Is this correct? Is there any other way to do this?
Here is how it looks like so far:
Thank you and here is my code.
First class is the info for frames and black rectangle.
Second class is the info for the blue rectangles and the walls.
First Class:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MyGame extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(5, this);
int x = 0, y = 0, velx =0, vely =0, g = 0;
private Color color;
public MyGame() {
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(1300, 750);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillRect(x, y, 50, 30);
}
#Override
public void actionPerformed(ActionEvent e) {
if (x < 0) //stops us from going backwards past x = 0
{
velx = 0;
x = 0;
}
if (y < 0) //stops us from going to the sky
{
vely = 0;
y = 0;
}
if (y > 725) // stops us from going through the ground
{
vely = 0;
y = 725;
}
if (x > 1250) // stops us from going through the wall
{
velx = 0;
x = 1250;
}
x += velx;
y += vely;
repaint();
}
#Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
{
if (code == KeyEvent.VK_DOWN) {
vely = 2; // removing velx = 0 allows us to go vertically and horizontlly at the same time
velx = 0;
}
if (code == KeyEvent.VK_UP) {
vely = -2; // same goes for here
velx = 0;
}
if (code == KeyEvent.VK_LEFT) {
vely = 0;
velx = -2;
}
{
if (code == KeyEvent.VK_RIGHT) {
vely = 0;
velx = 2;
}
}
}
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
public static void main (String arge[]){
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Incoming());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Second Class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Incoming extends MyGame {
private Color color;
int x = 0, y = 0;
int velx = 0, vely = 0;
public Incoming() {
color = Color.BLUE;
Rectangle rect = new Rectangle();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillRect(x, y, 50, 30);
g.setColor(Color.blue);
g.drawRect(0, 100, 80, 30);
g.drawRect(80, 100, 80, 30);
g.drawRect(160, 100, 80, 30);
g.drawRect(240, 100, 80, 30);
g.drawRect(320, 100, 80, 30);
g.drawRect(400, 100, 80, 30);
g.drawRect(480, 100, 80, 30);
g.drawRect(560, 100, 80, 30);
g.drawRect(640, 100, 80, 30);
g.drawRect(720, 100, 80, 30);
g.drawRect(800, 100, 80, 30);
g.drawRect(880, 100, 80, 30);
g.drawRect(960, 100, 80, 30);
g.drawRect(1040, 100, 80, 30);
g.drawRect(1040, 250, 80, 30);
g.drawRect(1120, 250, 80, 30);
g.drawRect(1200, 250, 80, 30);
g.drawRect(960, 250, 80, 30);
g.drawRect(880, 250, 80, 30);
g.drawRect(800, 250, 80, 30);
g.drawRect(720, 250, 80, 30);
g.drawRect(640, 250, 80, 30);
g.drawRect(560, 250, 80, 30);
g.drawRect(480, 250, 80, 30);
g.drawRect(400, 250, 80, 30);
g.drawRect(320, 250, 80, 30);
g.drawRect(240, 250, 80, 30);
g.drawRect(160, 250, 80, 30);
g.drawRect(1040, 400, 80, 30);
g.drawRect(960, 400, 80, 30);
g.drawRect(880, 400, 80, 30);
g.drawRect(800, 400, 80, 30);
g.drawRect(720, 400, 80, 30);
g.drawRect(640, 400, 80, 30);
g.drawRect(560, 400, 80, 30);
g.drawRect(480, 400, 80, 30);
g.drawRect(400, 400, 80, 30);
g.drawRect(320, 400, 80, 30);
g.drawRect(240, 400, 80, 30);
g.drawRect(160, 400, 80, 30);
g.drawRect(80, 400, 80, 30);
g.drawRect(0, 400, 80, 30);
g.drawRect(1040, 550, 80, 30);
g.drawRect(1120, 550, 80, 30);
g.drawRect(1200, 550, 80, 30);
g.drawRect(960, 550, 80, 30);
g.drawRect(880, 550, 80, 30);
g.drawRect(800, 550, 80, 30);
g.drawRect(720, 550, 80, 30);
g.drawRect(640, 550, 80, 30);
g.drawRect(560, 550, 80, 30);
g.drawRect(480, 550, 80, 30);
g.drawRect(400, 550, 80, 30);
g.drawRect(320, 550, 80, 30);
g.drawRect(240, 550, 80, 30);
g.drawRect(160, 550, 80, 30);
g.drawRect(1040, 550, 80, 30);
g.drawRect(960, 550, 80, 30);
g.drawRect(880, 550, 80, 30);
g.drawRect(800, 550, 80, 30);
g.drawRect(720, 550, 80, 30);
g.drawRect(640, 550, 80, 30);
g.drawRect(560, 550, 80, 30);
g.drawRect(480, 550, 80, 30);
g.drawRect(400, 550, 80, 30);
g.drawRect(320, 550, 80, 30);
g.drawRect(240, 550, 80, 30);
g.drawRect(160, 550, 80, 30);
}
#Override
public void actionPerformed(ActionEvent e) {
super.actionPerformed(e);
if (x < 0) //stops us from going backwards past x = 0
{
velx = 0;
x = 0;
}
if (y < 0) //stops us from going to the sky
{
vely = 0;
y = 0;
}
if (y > 725) // stops us from going through the ground
{
vely = 0;
y = 725;
}
if (x > 1250) // stops us from going through the wall
{
velx = 0;
x = 1250;
}
if (y < 0.1)
{
y = 50;
}
x += velx;
y += vely;
repaint();
}
#Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int code = e.getKeyCode();
{
if (code == KeyEvent.VK_S) {
vely = 2; // removing velx = 0 allows us to go vertically and horizontlly at the same time
velx = 0;
}
if (code == KeyEvent.VK_W) {
vely = -2; // same goes for here
velx = 0;
}
if (code == KeyEvent.VK_A) {
vely = 0;
velx = -2;
}
{
if (code == KeyEvent.VK_D) {
vely = 0;
velx = 2;
}
}
}
}
#Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
}
How would I list rectangles?
See the DrawOnCompnent example from Custom Painting Approaches. It shows you how to paint from an ArrayList.

Java - how to add a png file to JPanel

This Friday I need to hand in my Java project – I've only been programming for about two weeks now – and I really want to add a PNG file to my JPanel. Below you find my code, the name of the image is java.png, I put it in the same folder as my src files. I just can't find a good tutorial on how to do this. Please help me!
Note: the image should be inserted in the booOpAfbeam structure almost at the bottom of the code, and also sorry for this unorganized message, I have no clue how to post my code decently.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Image;
import java.awt.Dimension;
import javax.swing.*;
public class Project extends JPanel {
private JLabel invoerLabel, invoerKleur, invoerKleur2, invoerBril, invoerBril2;
private JTextField invoerAantal, invoerK, invoerB;
private int intAantal;
private String strKleur="";
private String strBril="";
private JButton knop, knopBril, btnOpAf, btnOpAfbeam;
private Boolean booOpAf = false, booOpAfbeam = false;
private Image image1;
public Project(){
setLayout(null);
invoerLabel = new JLabel("Voer aantal stoelen in: ");
invoerAantal = new JTextField (10);
invoerKleur = new JLabel("Voer kleur stoel in: ");
invoerKleur2 = new JLabel("(blauw, rood, groen, paars, roos, zwart, geel, oranje)");
invoerK = new JTextField (10);
invoerBril = new JLabel("Voer kleur bril in: ");
invoerBril2 = new JLabel("(blauw, rood, groen, paars, roos, zwart, geel, oranje)");
invoerB = new JTextField (10);
knopBril = new JButton ("ok");
knop = new JButton("ok");
btnOpAf = new JButton ("Wijs/Wijs niet");
btnOpAfbeam = new JButton ("aan/uit");
knop.addActionListener (new InvoervakHandler());
knopBril.addActionListener(new InvoervakHandler2());
btnOpAf.addActionListener(new aanuit());
btnOpAfbeam.addActionListener(new beam());
invoerLabel.setBounds(1000, 10, 150, 20);
invoerAantal.setBounds(1150, 10, 60, 20);
invoerKleur.setBounds(1000, 70, 120, 20);
invoerKleur2.setBounds(985, 90, 300, 20);
invoerK.setBounds(1150, 70, 60, 20);
knop.setBounds(1155, 120, 50, 20);
invoerBril.setBounds(1000, 170, 150, 20);
invoerBril2.setBounds(985, 195, 300, 20);
invoerB.setBounds(1150, 170, 60, 20);
knopBril.setBounds(1155, 220, 50, 20);
btnOpAf.setBounds(1000,275,115,20);
btnOpAfbeam.setBounds(365, 26, 75, 20);
add(invoerLabel);
add(invoerAantal);
add(invoerKleur);
add(invoerKleur2);
add(invoerK);
add(invoerBril);
add(invoerBril2);
add(invoerB);
add(knop);
add(knopBril);
add(btnOpAf);
add(btnOpAfbeam);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon i = new ImageIcon("java.png");
image1 = i.getImage();
g.drawImage(image1, 0, 0, null);
ImageIcon("java.png");
//labels//
invoerBril2.setForeground(new Color(0,173,173));
invoerKleur2.setForeground(new Color(0,173,173));
invoerLabel.setForeground(new Color(227,227,227));
invoerKleur.setForeground(new Color(227,227,227));
invoerBril.setForeground(new Color(227,227,227));
//vloer
g.setColor(new Color(102,77,51));
g.fillRect(0, 375, 1000, 200);
//muur
g.setColor(new Color(99,136,176));
g.fillRect(0, 0, 1000, 400);
//achtergrond kleur balk//
g.setColor(new Color(0,102,53));
g.fillRect(980, 0, 386, 766);
//beamer
g.setColor(new Color(122,122,122));
g.fillRect(355,20,100,30);
g.fillRect(397,0,15,40);
g.setColor(Color.BLACK);
g.fillRect(355, 20, 100, 3);
g.fillRect(355, 50, 100, 3);
g.fillRect(352, 20, 3, 33);
g.fillRect(452, 20, 3, 33);
g.fillRect(410, 0, 3, 20);
g.fillRect(395, 0, 3, 20);
//bord
g.setColor(new Color(98,121,74));
g.fillRect(250, 100,300 , 200);
g.setColor(Color.BLACK);
g.fillRect(250, 300,300 , 10);
g.fillRect(250, 100,300 , 10);
g.fillRect(250, 100,10 , 200);
g.fillRect(550, 100,10 , 210);
//boekenkast horizontale balken
g.setColor(Color.BLACK);
g.fillRect(50, 160, 30, 250);
g.fillRect(200, 160, 30, 250);
//boekenkast verticale balken
g.fillRect(50, 160, 150, 20);
g.fillRect(50, 220, 150, 20);
g.fillRect(50, 280, 150, 20);
g.fillRect(50, 340, 150, 20);
//boekenkast boeken rij 1
g.setColor(new Color(204,0,0));
g.fillRect(80, 180, 20, 40);
g.setColor(new Color(0,204,102));
g.fillRect(110, 180, 20, 40);
g.setColor(new Color(204,102,0));
g.fillRect(140, 180, 20, 40);
g.setColor(new Color(204,204,0));
g.fillRect(170, 180, 20, 40);
//boekenkast boeken rij 2
g.setColor(new Color(0,204,102));
g.fillRect(80, 240, 20, 40);
g.setColor(new Color(204,0,0));
g.fillRect(110, 240, 20, 40);
g.setColor(new Color(204,102,0));
g.fillRect(140, 240, 20, 40);
g.setColor(new Color(204,204,0));
g.fillRect(170, 240, 20, 40);
//boekenkast boeken rij 3
g.setColor(new Color(204,0,0));
g.fillRect(80, 300, 20, 40);
g.setColor(new Color(204,102,0));
g.fillRect(110, 300, 20, 40);
g.setColor(new Color(204,204,0));
g.fillRect(140, 300, 20, 40);
g.setColor(new Color(0,204,102));
g.fillRect(170, 300, 20, 40);
//boeklabels
g.setColor(new Color(224,224,224));
g.fillRect(85,190,10,20);
g.fillRect(115,190,10,20);
g.fillRect(145,190,10,20);
g.fillRect(175,190,10,20);
g.fillRect(85,250,10,20);
g.fillRect(115,250,10,20);
g.fillRect(145,250,10,20);
g.fillRect(175,250,10,20);
g.fillRect(85,310,10,20);
g.fillRect(115,310,10,20);
g.fillRect(145,310,10,20);
g.fillRect(175,310,10,20);
//hoofd//
g.setColor(new Color(255,237,184));
g.fillOval(615,170,150,150);
g.setColor(new Color(255,255,255));
g.fillOval(645,220,25,25);
g.fillOval(715,220,25,25);
g.setColor(new Color(0,0,0));
g.fillOval(655,230,10,10);
g.fillOval(725,230,10,10);
g.drawArc(675,240,40,40,0,-180);
g.drawArc(635,250,115,50,0,-180);
//lichaam
g.setColor(new Color(153,153,0));
g.fillRect(650, 300, 100, 125);
g.setColor(Color.BLACK);
g.fillRect(650, 430, 25, 60);
g.fillRect(730, 430, 25, 60);
g.fillRect(650, 420, 100, 15);
g.setColor(Color.BLUE);
g.fillOval(632, 470, 45, 25);
g.fillOval(725, 470, 45, 25);
// bureau
g.setColor(new Color(184, 184, 184));
g.fillRect(540, 410, 325, 20);
g.fillRect(540, 430, 20, 60);
g.fillRect(845, 430, 20, 60);
//vingers
g.setColor(new Color(255,237,184));
g.fillOval(785, 375, 10, 25);
g.fillOval(775, 375, 10, 25);
g.fillOval(765, 375, 10, 25);
g.fillOval(755, 375, 10, 25);
//pc
g.setColor(Color.BLACK);
g.fillRect(650, 280, 175, 100);
g.fillRect(676, 400, 125, 10);
g.fillRect(730, 375, 15, 30);
//
g.setColor(new Color(184, 184, 184));
g.fillRect(660, 320, 20, 5);
g.fillRect(660, 340, 20, 5);
g.fillRect(795, 320, 20, 5);
g.fillRect(795, 340, 20, 5);
//apple
g.fillOval(725, 320, 20, 20);
g.fillOval(732, 310, 5, 10);
g.setColor(Color.BLACK);
g.fillOval(735, 325, 10, 10);
if(strBril.equals("blauw")) {
g.setColor(Color.BLUE);
}
if(strBril.equals("rood")) {
g.setColor(Color.RED);
}
if(strBril.equals("groen")) {
g.setColor(Color.GREEN);
}
if(strBril.equals("paars")) {
g.setColor(new Color(204,51,255));
}
if(strBril.equals("roos")) {
g.setColor(new Color(255,51,204));
}
if(strBril.equals("zwart")) {
g.setColor(Color.BLACK);
}
if(strBril.equals("geel")) {
g.setColor(Color.YELLOW);
}
if(strBril.equals("oranje")) {
g.setColor(Color.ORANGE);
}{
//bril//
//linkerglas
g.fillRect(600,200,80,5);
g.fillRect(600,200,5,50);
g.fillRect(600,250,80,5);
g.fillRect(680,200,5,55);
//tussenbeentje//
g.fillRect(680,225,20,10);
//rechterglas
g.fillRect(700,200,80,5);
g.fillRect(700,200,5,50);
g.fillRect(700,250,80,5);
g.fillRect(780,200,5,55);
}
int teller1;
if(strKleur.equals("blauw")) {
g.setColor(Color.BLUE);
}
if(strKleur.equals("rood")) {
g.setColor(Color.RED);
}
if(strKleur.equals("groen")) {
g.setColor(Color.GREEN);
}
if(strKleur.equals("paars")) {
g.setColor(new Color(204,51,255));
}
if(strKleur.equals("roos")) {
g.setColor(new Color(255,51,204));
}
if(strKleur.equals("zwart")) {
g.setColor(Color.BLACK);
}
if(strKleur.equals("geel")) {
g.setColor(Color.YELLOW);
}
if(strKleur.equals("oranje")) {
g.setColor(Color.ORANGE);
}
int Ra = 20;
int Rb = 20;
int Rc = 40;
int Rd = 20;
for (teller1=1; teller1 <= intAantal; teller1++) {
g.fillRect(Ra,450,10,50);
g.fillRect(Rb,500,50,10);
g.fillRect(Rc,500,10,30);
g.fillRect(Rd,530,50,10);
Ra = Ra + 70;
Rb = Rb + 70;
Rc = Rc + 70;
Rd = Rd + 70;
}
if (booOpAf) {
if (!booOpAf) {
g.setColor(new Color(153,153,0));
g.fillRect(625, 300, 30, 100);
} else {
//arm
g.setColor(new Color(153,153,0));
g.fillRect(550, 315, 100, 25);
//linkerarm vingers
g.setColor(new Color(255,237,184));
g.fillOval(530, 310, 30, 30);
g.fillOval(515, 310, 25, 10);
g.fillOval(515, 320, 25, 10);
g.fillOval(515, 330, 25, 10);
g.fillOval(545, 300, 10, 25);
}
}
if (booOpAfbeam) {
if (!booOpAfbeam) {
} else {
g.drawImage(image1, 500, 100, null);
}
}
}
private void ImageIcon(String string) {
// TODO Auto-generated method stub
}
class InvoervakHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String strAantal = invoerAantal.getText();
intAantal = Integer.parseInt(strAantal);
strKleur= invoerK.getText();
repaint();
}
}
class InvoervakHandler2 implements ActionListener {
public void actionPerformed(ActionEvent f){
strBril= invoerB.getText();
repaint();
}
}
public class aanuit implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (booOpAf == true) {
booOpAf = false;
} else {
booOpAf = true;
}
repaint();
}
}
public class beam implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
if (booOpAfbeam == true) {
booOpAfbeam = false;
} else {
booOpAfbeam = true;
}
repaint();
}
}
}
I cant verify your complete code. so better ensure that u have imported the image to the project(else u wont get the image if the image is not in the location specified). and also check if the image is in the src, u need to specify "/" and the image name. to locate the image.
I suggest the best way is to create a label,the right click and take the property of the image and select the image which u wants to display. rest of the codes netbeans will take care of.
Sorry if i made any mistake.
Try this:
Add another panel and then upload image on that panel.
lblImage = new javax.swing.JLabel();
lblImage.setIcon(new javax.swing.ImageIcon("E:..path...png));

Rotating a shape vertically around the x-axis

I have a 2d graph with an x and y axis and im trying to rotate a shape (series of points) around an axis. This rotation will need to include a scale function.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import javax.swing.*;
import java.lang.reflect.Array;
public class test extends JPanel implements ActionListener {
int[] p1x = {200, 200, 240, 240, 220, 220, 200};
int[] p1y = {200, 260, 260, 240, 240, 200, 200};
int[] p2x = {600, 600, 620, 620, 640, 640, 660, 660, 600};
int[] p2y = {400, 420, 420, 460, 460, 420, 420, 400, 400};
int[] p3x = {400, 400, 460, 460, 440, 440, 420, 420, 400};
int[] p3y = {400, 460, 460, 400, 400, 440, 440, 400, 400};
int delay = 1000;
int dx = 0;
int dy = 5;
int steps = 121;
Polygon t;
Timer tim = new Timer(delay, this);
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < Array.getLength(p2x); i++) {
//p2x[i] = (int) (p2x[i]*Math.cos(Math.toRadians(1))- p2y[i]*Math.sin(Math.toRadians(1)));
//p2y[i] = (int) (p2x[i]*Math.sin(Math.toRadians(1))+ p2y[i]*Math.cos(Math.toRadians(1)));;
Point2D original = new Point2D.Double(p2x[i], p2y[i]);
AffineTransform at = new AffineTransform();
//at.setToRotation(.02, 250, 250);
at.scale(1, -1);
Point2D rotated = at.transform(original, null);
p2x[i] = (int) rotated.getX();
p2y[i] = (int) rotated.getY();
}
repaint();
if (--steps == 0) {
tim.stop();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
g.drawLine(this.getWidth() / 2, 0, this.getWidth() / 2, this.getWidth());
g.drawLine(0, this.getHeight() / 2, this.getHeight(), this.getHeight() / 2);
Polygon t = new Polygon(p2x, p2y, 9);
g.drawPolygon(t);
Letters u = new Letters(p3x, p3y, 9);
u.draw(g);
Letters l = new Letters(p1x, p1y, 7);
l.draw(g);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Drawing line and a moving polygon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test sl = new test();
frame.getContentPane().add(sl);
frame.setSize(700, 700);
frame.setVisible(true);
sl.tim.start();
}
}
Absent a clear question, a simple animation using your coordinate arrays is shown below. In general you can transform the graphics context (g2d) or the polygonal Shape itself (p3); the example shows both. Resize the window to see the effect of each.
Note the last-specified-first-applied order of the transformations in at. First, a suitable point on p3 is translated to the origin, then p3 is scaled, and then p3 is translated to the center of the panel. The + 10 fudge factor applied to p3 is an artifact of having no symmetric rotation point. It may be easier to define your polygons relative to the origin, as shown in this example.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;
/** #see http://stackoverflow.com/questions/3405799 */
public class AffineTest extends JPanel implements ActionListener {
private static final double DELTA_THETA = Math.PI / 45; // 4°
private static final double DELTA_SCALE = 0.1;
private int[] p1x = {200, 200, 240, 240, 220, 220, 200};
private int[] p1y = {200, 260, 260, 240, 240, 200, 200};
private int[] p2x = {600, 600, 620, 620, 640, 640, 660, 660, 600};
private int[] p2y = {400, 420, 420, 460, 460, 420, 420, 400, 400};
private int[] p3x = {400, 400, 460, 460, 440, 440, 420, 420, 400};
private int[] p3y = {400, 460, 460, 400, 400, 440, 440, 400, 400};
private Polygon p1 = new Polygon(p1x, p1y, p1x.length);
private Polygon p2 = new Polygon(p2x, p2y, p2x.length);
private Polygon p3 = new Polygon(p3x, p3y, p3x.length);
private AffineTransform at = new AffineTransform();
private double dt = DELTA_THETA;
private double theta;
private double ds = DELTA_SCALE;
private double scale = 1;
private Timer timer = new Timer(100, this);
public AffineTest() {
this.setPreferredSize(new Dimension(700, 700));
this.setBackground(Color.white);
p1.translate(-50, +100);
p2.translate(-100, -100);
}
#Override
public void actionPerformed(ActionEvent event) {
theta += dt;
scale += ds;
if (scale < .5 || scale > 4) {
ds = -ds;
}
repaint();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = this.getWidth();
int h = this.getHeight();
g2d.drawLine(w / 2, 0, w / 2, h);
g2d.drawLine(0, h / 2, w, h / 2);
g2d.rotate(theta, w / 2, h / 2);
g2d.drawPolygon(p1);
g2d.drawPolygon(p2);
at.setToIdentity();
at.translate(w / 2, h / 2);
at.scale(scale, scale);
at.translate(-p3x[5] + 10, -p3y[5]);
g2d.setPaint(Color.blue);
g2d.fill(at.createTransformedShape(p3));
}
public void start() {
timer.start();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Affine Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
AffineTest sl = new AffineTest();
frame.add(sl);
frame.pack();
frame.setVisible(true);
sl.start();
}
}

Categories

Resources