I am not very good with using multiple classes in java, as I've always found it easier to do all of my code in 1 class. Recently I've found the need to use a second class for a game I'm making and I'm running into an error.
Right now I'm just trying to spawn the enemy where and when the user clicks.
Main Class -
package joey.rts;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class RTSMain extends JFrame implements MouseListener{
/**
*
*/
private static final long serialVersionUID = -7122370886923000314L;
public static BufferedImage menu,enemy;
public static boolean onmenu,oneenemy;
public static void main(String[] args){
new RTSMain();
}
public RTSMain(){
init();
}
public void init(){
setSize(1700,1100);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("RTS");
addMouseListener(this);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
if(onmenu == true){
g2.drawImage(menu,0,0,this);
}
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
Enemy enemy = new Enemy();
int x = e.getX();
int y = e.getY();
enemy.spawnEnemy(x, y);
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
Enemy class -
package joey.rts;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Enemy{
/**
*
*/
public static BufferedImage enemy;
private static final long serialVersionUID = 7898827977636314494L;
public static RTSMain rts;
public static void main(String[] args){
try{
enemy = ImageIO.read(new File(javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory() + "\\enemy.png"));
} catch (Exception e){
e.printStackTrace();
}
}
public static void spawnEnemy(int x, int y){
Graphics g = rts.getGraphics();
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(enemy,x,y,null);
}
}
In your Main class update your mouseClicked function to be :
#Override
public void mouseClicked(MouseEvent e) {
Enemy enemy = new Enemy();
int x=e.getX(); // get mouse positionX
int y=e.getY();//get mouse positionY
enemy.spawnEnemy(x,y);//spawn Enemy
}
Consider saving the enemy objects if you need to reuse it later.
Also I see no need to extend anything in Enemy Class.
I've Updated Your Main and your Enemy class :
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Main extends JFrame implements MouseListener{
/**
*
*/
private static final long serialVersionUID = -7122370886923000314L;
public static BufferedImage menu,enemy;
public static boolean onmenu,oneenemy;
public static void main(String[] args){
new Main().setVisible(true);
}
public Main(){
init();
}
public void init(){
setSize(1700,1100);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("RTS");
addMouseListener(this);
}
#Override
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
if(onmenu == true){
g2.drawImage(menu,0,0,this);
}
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
Enemy enemy = new Enemy();
int x = e.getX();
int y = e.getY();
enemy.spawnEnemy(x, y,this.getGraphics());
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
And this is the Enemy class :
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class Enemy{
public static BufferedImage enemy;
private static final long serialVersionUID = 7898827977636314494L;
public Enemy(){
try {
//MAKE SURE THAT THIS IS THE CORRECT IMAGE PATH
enemy = ImageIO.read(new File(javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory() + "\\enemy.png"));
} catch (IOException ex) {
Logger.getLogger(Enemy.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void spawnEnemy(int x, int y,Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(enemy,x,y,null);
}
}
I've removed the instance variable of Main that was on Enemy class.
I've removed the static modifier for the spawnEnemy function.
I've sent the graphics object as an attribute to spawnEnemy function .
I've Moved the code that was in main method in Enemy class to the Enemy Constructor.
Hope it helps !
It just seemed easier to paste an answer than to try to explain it in a comment. Your OO is a bit off. Add this to your RTSMain class:
protected ArrayList<Enemy> enemies = new ArrayList<Enemy>();
protected BufferedImage enemyImage = null;
...
public void init() {
...
// everything you already have...
enemyImage = //read in your enemy image here
...
}
...
#Override
public void mouseClicked(MouseEvent e) {
// this takes place of Enemy.spawn(), get rid of it
int x = e.getX(); // get mouse positionX
int y = e.getY(); //get mouse positionY
Enemy enemy = new Enemy( enemyImage, x, y );
Enemies.add( enemy );
// Iterate over list above to draw each enemy in your paintComponent method
}
Also, JFrame doesn't have a paintComponent() method, which you need to override to do your painting (paint() is deprecated). Add a JPanel, override it's paintComponent() and so on. For a game, you'll probably want to create a game loop to do your painting, unless it's very turn-based.
Happy coding.
Related
So I'm trying to draw a circle where a user clicks, which can then be re-sized by the bar down below. Everything works except for that the circle won't draw where I want it to. Any suggestions?
Here is my Panel
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.event.*;
public class TestClass extends JFrame {
private JSlider slide;
private MainClass myPanel;
public int x1=0;
public int y1=0;
public TestClass(){
super("The Title");
myPanel = new MainClass();
myPanel.setBackground(Color.YELLOW);
slide = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
slide.setMajorTickSpacing(10);
slide.setPaintTicks(true);
slide.addChangeListener(
new ChangeListener(){
public void stateChanged(ChangeEvent e){
myPanel.checkDiameter(slide.getValue());
}
}
);
HandlerClass handler = new HandlerClass();
slide.addMouseListener(handler);
add(slide, BorderLayout.SOUTH);
add(myPanel, BorderLayout.CENTER);
}
public int setX1(){
return x1;
}
public int setY1(){
return y1;
}
private class HandlerClass implements MouseListener{
#Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
x1=event.getX();
y1=event.getY();
repaint();
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
Here is the other important class which creates a window and call TestClass;
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MainClass extends JPanel{
private int d = 10;
public void paintComponent(Graphics g){
super.paintComponent(g);
TestClass values = new TestClass();
g.setColor(Color.CYAN);
g.fillOval(values.setX1()+50, values.setY1(), d, d);
}
public void checkDiameter(int newD)
{
//New format for if statements
d = (newD >= 0 ? newD //if
: 10//else
);
repaint();
}
public Dimension getPreferredSize(){
return new Dimension(200,200);
}
public Dimension getMinimumSize(){
return getPreferredSize();
}
}
Hey Guys I found a way to get it working.
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.event.*;
public class TestClass extends JFrame {
private JSlider slide;
private MainClass myPanel;
public static int x1=0,y1=0;
public TestClass(){
super("The Title");
myPanel = new Panel();
myPanel.setBackground(Color.YELLOW);
//Allows you to re-size the drawn circle
slide = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
slide.setMajorTickSpacing(10);
slide.setPaintTicks(true);
slide.addChangeListener(
new ChangeListener(){
public void stateChanged(ChangeEvent e){
myPanel.checkDiameter(slide.getValue());
}
}
);
//Create a way to handle user mouse events
HandlerClass handler = new HandlerClass();
myPanel.addMouseListener(handler);
add(slide, BorderLayout.SOUTH);
add(myPanel, BorderLayout.CENTER);
}
private class HandlerClass implements MouseListener{
#Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
Right here is what is new with the code, this method gets the x and y coordinates of a click and sends it to MainClass object myPanel which has the setPosition method; class show before.
myPanel.setPosition(event.getX(),event.getY());
repaint(); }
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
Here is the MainClass class, it is poorley named as it's not actually the main class...
import java.awt.;
import javax.swing.;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Panel extends JPanel{
private int x1,y1;
private int d = 10;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.CYAN);
g.fillOval(x1, y1, d, d);
}
public void checkDiameter(int newD)
{
//New format for if statements
d = (newD >= 0 ? newD //if
: 10//else
);
repaint();
}
public void setPosition(int newX, int newY) {
this.x1 = newX;
this.y1 = newY;
repaint();
}
public Dimension getPreferredSize(){
return new Dimension(200,200);
}
public Dimension getMinimumSize(){
return getPreferredSize();
}
}
I'm trying to make checkers game but the following architecture does not show on JFrame what I'm doing wrong
//base class
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public abstract class Case extends JComponent implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
protected static final int LONGUEUR=40;
protected int x,y,width,height;
#Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
//black rectangle
import java.awt.Color;
import java.awt.Graphics;
public class CaseNoire extends Case
{
/**
*
*/
private static final long serialVersionUID = 1L;
public CaseNoire(int x, int y,int width,int height)
{
this.x=x;
this.y=y;
this.width = width;
this.height= height;
}
#Override
protected void paintComponent(Graphics g)
{
g.setColor(Color.black);
g.fillRect(x, y,width,height);
}
}
//white rectangle
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
public class CaseBlanche extends Case {
/**
*
*/
private static final long serialVersionUID = 1L;
public CaseBlanche(int x, int y,int width,int height)
{
this.x=x;
this.y=y;
this.width = width;
this.height= height;
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(x, y,width,height);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Paint;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class Plateau extends JComponent
{
private Case[][] cases= new Case[10][10];
#Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
for(int i=0; i<10; i++)
{
for(int j=0;j<10;j++)
{
if((i+j)%2==0)
{
CaseBlanche blanche= new CaseBlanche(j*Case.LONGUEUR,i*Case.LONGUEUR,Case.LONGUEUR, Case.LONGUEUR);
//cases[i][j]=blanche;
add(blanche);
//g.setColor(Color.white);
// g.fillRect(j*40, i*40,40,40);
}
else
{
CaseNoire caseNoire=new CaseNoire(j*Case.LONGUEUR,i*Case.LONGUEUR,Case.LONGUEUR, Case.LONGUEUR);
add(caseNoire);
}
}
}
}
}
and here the Main Class
import javax.swing.JFrame;
public class MainDamesFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args )
{
MainDamesFrame damesFrame = new MainDamesFrame();
}
public MainDamesFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Jeu de Dames");
this.getContentPane().add(new Plateau());
setSize(800, 600);
setVisible(true);
}
}
I expect something like this
So what's wrong with my code? why paintComponent in CaseBlanche and CaseNoire does not behave as expected?
Please help
public void paintComponents(Graphics g)
You have a typo. Get rid of the "s" in paintComponents().
setSize(800, 600);
This won't work as you expected because the (880, 600) included the title bar and borders, so some of the squares will not be painted properly.
So, your custom painting classes should override the getPreferredSize() method to return the size of each component. Then you can use the pack() method to make sure the components are properly sized.
There is no need for the CaseNoir and CaseBlanche classes. When you create the Case object you can just use:
setBackground(Color.BLACK);
Then when you paint the component you just use:
g.setColor( getBackground() );
Also, your Plateau class is wrong. The paintComponent() method is for painting. Your code is creating and adding components components. If you want to add components then you should be using a layout manager. The GridLayout will easily add components in a grid as long as the getPreferredSize() method has been implemented. You create the component with a specified width/height, so to implement this method you do something like:
#Override
public Dimension getPreferredSize()
{
return new Dimension(width, height)
}
Check out this Chessboard Example for a way to add components in a grid.
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.*;
import java.util.*;
public class test1 extends JFrame implements MouseListener {
private JPanel JP = new JPanel();
public test1() {
JP.setBorder(BorderFactory.createLineBorder(Color.black));
JP.addMouseListener(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.add(JP);
this.pack();
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
test1 frame = new test1();
frame.setSize(400,400);
frame.setVisible(true);
}
});
}
public void mouseClicked(MouseEvent e) {
//drawCircle(e.getX(), e.getY());
//repaint();
ballball ball;
ball = new ballball();
//ball.paintComponent(Graphics g);
System.out.println("ballball");
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
//this.mouseX=e.getX();
//this.mouseY=e.getY();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
}
class ballball extends test1 implements Runnable {
private int squareX = 50;
private int squareY = 50;
private int squareW = 100;
private int squareH = 100;
public boolean draw;
private Vector<Object> v = new Vector<Object>();
public ballball() {
/*addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
draw = true;
//Thread thread1 = new Thread(this.moveSquare(50, 50));
repaint();
//moveSquare(e.getX(),e.getY());
}
});*/
/*addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
moveSquare(e.getX(),e.getY());
}
});*/
System.out.println("ball created");
this.repaint();
}
public void run() {
}
private void moveSquare(int x, int y) {
int OFFSET = 1;
if ((squareX!=x) || (squareY!=y)) {
repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
squareX=x;
squareY=y;
repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
}
}
public void paint(Graphics g) {
g.drawString("abcasdfasffasfas", 10, 10);
}
//#Override
public void paintComponent(Graphics g) {
//if (draw) {
// existing code
System.out.println("paint");
//super.paintComponent(g);
//g.drawString("This is my custom Panel!",10,20);
//g.setColor(Color.RED);
//g.fillRect(squareX,squareY,squareW,squareH);
//g.setColor(Color.BLACK);
//g.drawRect(squareX,squareY,squareW,squareH);
Shape circle = new Ellipse2D.Float(squareX,squareY,100f,100f);
Graphics2D ga = (Graphics2D)g;
ga.draw(circle);
//}
}
}
The aim of the program is to click to create the circle, the ballball class extends the test1, when test1 detect the mouse click, the ballball object created. But the paint/paintComponent method is never be executed. In my program structure, is it possible to paint the circle to the super class JPanel?
JFrame is not a JComponent, it doesn't have a paintComponent method you can override. Instead you could extend a JPanel and add it to the frame.
I am trying to create a panel where I will have the possibility to zoom on custom made JComponent objects.
I have tried to call the scale() method in the AffineTransform class with different values, but I have not succeeded. My objects just disappear.
Below is my component that is used in the main frame class. Everything works except the zooming. Could some of you explain the concepts of AffineTransform. I donĀ“t think the JavaDoc explenation is enough for me.
Here is an executable SSCCE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class JavaApplication1 extends JFrame {
public static void main(String[] args) {
new JavaApplication1();
}
private MyComponent myComponent = new MyComponent();
public JavaApplication1() throws HeadlessException {
this.setSize(400,400);
this.setVisible(true);
this.add(myComponent);
}
class MyComponent extends JComponent {
private int x, y;
private double scale=1;
private MouseAdapter mouseAdapter = new MouseAdapter();
private AffineTransform transform = new AffineTransform();
public MyComponent() {
this.addMouseListener(mouseAdapter);
this.addMouseWheelListener(mouseAdapter);
this.addMouseMotionListener(mouseAdapter);
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.DARK_GRAY);
g2.fillRect(0, 0, 400, 400);
g2.setColor(Color.RED);
g2.setTransform(transform);
transform.scale(scale, scale);
g2.drawString("My String!", x, y);
}
private class MouseAdapter implements MouseWheelListener, MouseListener, MouseMotionListener {
#Override
public void mouseWheelMoved(MouseWheelEvent e) {
if(e.getWheelRotation() == 1) {
scale+=0.1;
}else {
scale-=0.1;
}
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
#Override
public void mouseMoved(MouseEvent e) {
}
}
}
}
transform.scale accepts two double parameters, one for the x axis and the other for the y axis, being 1 the neutral value (no change in scale), and the two parameters the multipliers.
Ex: transform.scale(2,2) will show the component twice as big, while transform.scale(0.5,0.5) will show it twice as small.
http://docs.oracle.com/javase/6/docs/api/java/awt/geom/AffineTransform.html#scale(double, double)
Anybody know how can I repaint in a clone JPanel. Im using CLONE, so I can REPAINT() one, and the other will do the same automatically.
My code only paints the original JPanel if I move the mouse in the original or in the clone panel,
but If I move the mouse in the clone panel, this jpanel doesn't paint.
Thanks in advance
CODE:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ProcessorClone {
public static void main(String[] args) {
JFrame aWindow = new JFrame();
aWindow.setBounds(300, 200, 300, 100);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Container content = aWindow.getContentP
aWindow.setVisible(true);
CardViewer panel02=new CardViewer();
CardViewer panel01= (CardViewer) panel02.clone();
aWindow.setLayout(new BorderLayout());
aWindow.add(panel01,BorderLayout.NORTH);
aWindow.add(panel02,BorderLayout.SOUTH);
panel01.setBackground(Color.RED);
panel02.setBackground(Color.blue);
panel01.repaint();
panel02.repaint();
panel01.validate();
panel02.validate();
}
}
class CardViewer extends JPanel implements MouseListener,MouseMotionListener, Cloneable {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel mContentPaneBorder;
private JPanel mContentPane;
private JButton FileButton=new JButton("AAA");
private Point p;
public CardViewer(){
super();
this.add(FileButton);
addMouseListener(this);
addMouseMotionListener(this);
}
#Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("mouseClicked");
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
System.out.println("mousePressed");
p = null;
repaint();
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseMoved(MouseEvent e) {
System.out.println("mouseMoved");
p = e.getPoint();
this.repaint();
this.validate();
}
public void paint(Graphics g) {
System.out.println( g.getClass() );
if (p != null) {
Dimension d = getSize();
int xc = d.width / 2;
int yc = d.height / 2;
g.drawLine(p.x, p.y, p.x, p.y);
this.setBackground(Color.pink);
}
}
public Object clone () {
// First make exact bitwise copy
CardViewer copy = null;
try {
copy = (CardViewer)super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return copy;
}
}
.clone() does not return a mirror of the JPanel but returns a copy of the object, so you really have 2 separate JPanel objects so actions in one will not automatically reflect in the other.
You could override all the actions in a component that inherits from JPanel with a reference to a .cloned() JPanel object and then route all methods to the other JPanel after calling super()