I have a problem, when i call repaint() on JDialog, I see nothing on the screen, but when i move the JDialog by my self, I see what i wanted to paint.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class dude extends JFrame {
private static int cnt = 0;
public dude() {
super("ff");
makeFrame();
}
public void makeFrame() {
new Dialog(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setSize(400, 400);
setVisible(true);
}
private class Dialog extends JDialog {
public Dialog(JFrame frame) {
super(frame, "ff", true);
makeFrame();
}
public void makeFrame() {
getContentPane().addMouseListener(new M(this));
setDefaultCloseOperation(HIDE_ON_CLOSE);
pack();
setLocation(200, 200);
setSize(400, 400);
setVisible(true);
}
private class M extends MouseAdapter {
private JDialog dialog;
public M(JDialog dialog) {
this.dialog = dialog;
}
public void mouseClicked(MouseEvent e) {
P p = new P(e.getX(), e.getY());
p.repaint();
dialog.add(p);
}
private class P extends JPanel {
private int x, y;
public P(int x, int y) {
this.x = x;
this.y = y;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawOval(x, y, 10, 10);
}
/*public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawOval(x,y,10,10);
}*/
}
}
}
}
That's weirdest piece of code I've seen for a while, but. You're immeditate problem is with you mouseClicked event...
Replace your p.repaint call with a call to the dialogs revalidate method.
P p = new P(e.getX(), e.getY());
dialog.add(p);
dialog.revalidate();
Your repaint method would have done nothing any way, it was being called before you panel was realized (connected to the screen)
Seems like you need to look into the coding style you adhering to. Though, leave that for latter part, simply add this method to your M Class
public void setValues(int x, int y)
{
this.x = x;
this.y = y;
repaint();
}
And make p an Instance Variable of your Dialog Class. And inside your mouseClicked() method, simply call this method. And remove the constructor part, since you initializing a new JPanel for each drawing which I guess is not good in any sense. When you simply can draw the new thingy on the same JPanel
Related
I'm trying to make the game snake and have run into an issue. What I have is a class called Segment which is used to create the objects that hold the x- and y-positions and also the direction of the snake. The class extends JPanel and overrides the method paintComponent(). I then add an object of type Segment to a JFrame in a different class. The methods I have for moving/changing directions of the snake (actually just a square at the moment) work perfectly but my problem is this:
When the square gets to about half of the width of the frame or half of the height of the frame it stops being drawn. I have made the background of the JPanel light grey so I know that the square hasn't reached the end of the JPanel when it stops. Below is my simple paintComponent() method and the section in the class that extends JFrame where I add the object.
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.LIGHT_GRAY);
g.setColor(Color.black);
g.fillRect(xpos, ypos, width, height);
}
public Snake() {
setLayout(new BorderLayout());
addKeyListener(l);
segment = new Segment(100, 100, Segment.Dir.RIGHT);
segment.setPreferredSize(new Dimension(500,500));
add(segment, BorderLayout.CENTER);
timer.start();
setVisible(true);
pack();
setTitle("Snake");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
I also know that the objects position doesn't stop being updated so it's just the drawing of the square in the entire panel/frame that's the issue. Appreciate any help!
Here's an MCVE:
import java.awt.*;
import javax.swing.*;
public class Segment extends JPanel {
private int width = 10;
private int height = 10;
private int xpos, ypos;
private Dir dir;
public enum Dir {
LEFT, RIGHT, UP, DOWN;
}
public Segment() {
xpos = 0;
ypos = 0;
dir = Dir.RIGHT;
}
public Segment(int x, int y, Dir d) {
xpos = x;
ypos = y;
dir = d;
}
public Dir getDir() {
return dir;
}
public void setDir(Dir d) {
dir = d;
}
public void setX(int x) {
xpos = x;
repaint();
}
public void setY(int y) {
ypos = y;
repaint();
}
public int getX() {
return xpos;
}
public int getY() {
return ypos;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.LIGHT_GRAY);
g.setColor(Color.black);
g.fillRect(xpos, ypos, width, height);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Snake extends JFrame implements ActionListener {
Segment segment;
Timer timer = new Timer(50, this);
public Snake() {
setLayout(new BorderLayout());
setSize(500,500);
segment = new Segment(100, 100, Segment.Dir.RIGHT);
segment.setPreferredSize(new Dimension(getWidth(),getHeight()));
add(segment, BorderLayout.CENTER);
timer.start();
setVisible(true);
setTitle("Snake");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == timer) {
segment.setX((segment.getX() + 4 + getWidth())%getWidth());
}
}
public static void main(String[] arg) {
new Snake();
}
}
Don't override getX() and getY() of the Segment class.
Those are methods of all Swing components. They get the current location of the component within the parent container.
Use different method names to control the location of the painting of your snake. Since your variable names are xPos and yPos maybe use getXPos() and getYPos().
So what happens is that the snake is drawn at xPos/yPos relative to the Segment panel and the Segment panel is also drawn at xPos/yPos relative to its parent container.
The problem here is that the paintComponent() method gets called, it gets the necessary variables for fillRect() but doesn't actually draw anything after a key is pressed. I don't understand why since the returned value of mato.getPositionX() does get incremented every time the D key is pressed and the incremented value gets passed to fillRect(). Here's the code:
Screen class
public class Screen extends JFrame implements KeyListener {
private Mato mDrawScreensMato;
public Screen() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
setLocationRelativeTo(null);
setSize(400, 400);
DrawScreen screen = new DrawScreen();
mDrawScreensMato = screen.getMato();
addKeyListener(this);
add(screen);
}
//keyTyped
#Override
public void keyPressed(KeyEvent ke) {
int c = ke.getKeyCode();
if (c == KeyEvent.VK_D) {
mDrawScreensMato.setPositionX(mDrawScreensMato.getPositionX() + 1);
repaint();
}
}
//keyReleased
}
DrawScreen class
public class DrawScreen extends JPanel {
private Mato mato;
public DrawScreen() {
mato = new Mato();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
System.out.println(mato.getPositionX());
g2d.fillRect(
mato.getPositionX(), mato.getPositionY(),
mato.MATO_WIDTH, mato.MATO_HEIGHT
);
}
public Mato getMato() {
return mato;
}
}
Mato class
public class Mato {
final int MATO_WIDTH = 20;
final int MATO_HEIGHT = 20;
final int MATO_START_POS_X = 20;
final int MATO_START_POS_Y = 40;
private int positionX;
private int positionY;
public Mato(){
positionX = MATO_START_POS_X;
positionY = MATO_START_POS_Y;
}
public void setPositionX(int positionX) {
this.positionX = positionX;
}
public int getPositionX() {
return positionX;
}
//Get/Set positionY
}
The main cause of your problem is you are calling setVisible to early...
The general rule of thumb is, call setVisible only after you have prepared the UI
public Screen() {
DrawScreen screen = new DrawScreen();
mDrawScreensMato = screen.getMato();
addKeyListener(this);
add(screen);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// This is a useless call as DrawScreen
// does not provide appropriate sizing hints to the layout manager
pack();
setSize(400, 400);
// This needs to be called AFTER the size of window has been determined,
// as it uses the size of the window to determine it's location
setLocationRelativeTo(null);
setVisible(true);
}
KeyListener is notoriously troublesome, you would better of using Key Bindings
It won't paint at all, any ideas?
Nothing is displayed on the back panel, how do I get to paint with the mouseDragged event?
I can't even display a single line with that... Here's the source code.. I added the Jbutton just to see if the Panel was actually being displayed
public class pinta extends JFrame {
HandlerClass handler=new HandlerClass();
JPanel back=new JPanel();
public pinta(){
setSize(500,500);
setResizable(true);
getContentPane().setLayout(new BorderLayout());
back.setBackground(Color.white);
back.setSize(500,500);
this.add(back);
back.add(new JButton("test"));
back.addMouseMotionListener(handler);
back.setOpaque(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g){
super.paintComponents(g);
g.setColor(Color.black);
Graphics2D g2d = (Graphics2D)g;
g2d.fillOval(100, 100, 20, 10);
g2d.setPaintMode();
g2d.setStroke(new BasicStroke(1));
}
public class HandlerClass implements MouseMotionListener{
int x, y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void mouseDragged(MouseEvent e) {
x=e.getX();
y=e.getY();
}
public void mouseEntered(MouseEvent e){
}
public void mouseMoved(MouseEvent e) {
}
}
}
JFrame does not have a method call paintComponent. If you used the #Override annotation, the compiler would have failed.
Also note, you are calling super.paintComponents - Notice the "s" at the end, this should have being an interactor of problems
JComponent (or JPanel which extends JComponent) is what you're after.
Take a look at Performing Custom Painting for more details
I should also mention that back.setSize(500,500) is irrelevant, as the layout manager will decide what size it wants to make the component
I want to see all the Points one after another but I see only able to see 1
point. What shold I change to see all the Points ?
In the System.out you can see 10 times "set" and then 2 times
"paintComponent". what should I change that after each time set is
called it change the "paintComponente" ?
==================================================================================
public class exampe extends JPanel
{
int x;
int y;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fillOval(x-2,y-2,4,4);
System.out.println("paintComponent");
}
public void set(int X, int Y)
{
x = X;
y = Y;
System.out.println("set");
super.repaint();
}
public static void main(String args[])
{
int e=1;
JFrame frame = new JFrame("TEST");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exampe ex= new exampe();
JScrollPane scroll = new JScrollPane(ex);
frame.getContentPane().add(scroll);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
for(int i=0; i< 10; ++i)
ex.set(e+i,e+i);
}
}
*SIMPLE EXPLANATION AS TO WHY YOU COULD ONLY SEE THE LAST UPDATE : *
A quote taken from Filthy Rich Clients by Chet Haase and Romain Guy
It is important to note that repaint requests get “coalesced,” or combined.
So, for example, if you request a repaint and there is already one on the
queue that has not yet been serviced, then the second request is ignored
because your request for a repaint will already be fulfilled by the earlier
request. This behavior is particularly helpful in situations where many
repaint requests are being generated, perhaps by very different situations
and components, and Swing should avoid processing redundant requests and
wasting effort.
Try your hands on this, and ask what is not clear to you :
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class PointsExample
{
private CustomPanel contentPane;
private Timer timer;
private int x = 1;
private int y = 1;
private ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
contentPane.set(x, y);
x++;
y++;
if (x == 450)
timer.stop();
}
};
/*
* This is just JFrame, that we be
* using as the Base for our Application.
* Though here we are calling our
* JPanel (CustomPanel), whose
* paintComponent(...) method, we had
* override.
*/
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Locate Mouse Position");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new CustomPanel();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
timer = new Timer(100, timerAction);
timer.start();
}
public static void main(String\u005B\u005D args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PointsExample().createAndDisplayGUI();
}
});
}
}
class CustomPanel extends JComponent
{
private int x;
private int y;
public void set(int a, int b)
{
x = a;
y = b;
repaint();
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
#Override
public void paintComponent(Graphics g)
{
g.clearRect(0, 0, getWidth(), getHeight());
Graphics2D g2 =(Graphics2D) g;
g2.fillOval(x, y, 4, 4);
}
}
Here is the code, that will allow you to have a look at your points while iterating inside a for loop, though this approach is highly discouraged, for many cons associated with it. Though try your hands on this instead of calling repaint() call paintImmediately(int ...) or paintImmediately(Rectangle rect)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class PointsExample
{
private CustomPanel contentPane;
private Timer timer;
private int x = 1;
private int y = 1;
/*
* This is just JFrame, that we be
* using as the Base for our Application.
* Though here we are calling our
* JPanel (CustomPanel), whose
* paintComponent(...) method, we had
* override.
*/
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("Locate Mouse Position");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new CustomPanel();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
for (int i = 0; i < 500; i++)
{
contentPane.set(x, y);
x++;
y++;
if (x == 450)
break;
}
}
public static void main(String\u005B\u005D args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PointsExample().createAndDisplayGUI();
}
});
}
}
class CustomPanel extends JComponent
{
private int x;
private int y;
public void set(int a, int b)
{
x = a;
y = b;
paintImmediately(0, 0, getWidth(), getHeight());
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(500, 500));
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillOval(x, y, 4, 4);
}
}
1: first line of paintComponent() should be your super.paintComponent()
2: why are you calling super.repaint(), make it simply repaint()
Your Drow should be like this.
public class drow extends JPanel {
...........
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 =(Graphics2D) g;
}
public void set_list(LinkedList <point> p){
Points =p;
repaint();
}
try with this.
i hope this is simply a structure, your paintComponent() isn't drawing anything.
EDIT
public void set_list(LinkedList <point> p){
Points =p;
System.out.println("set_ist");// 1:First this line will be displayed then..
repaint();//2: Then this is called, which in turn calls your `paintComponent()`
}
Now when your paintComponent() is called it has
system.out.println("paintComponent");
//3: so now this will be displayed.
Where is the problem here?
EDIT- SWING TIMER
Your code was ok, but the function processing is way faster than GUI updation, thats why you were unable to see the changes in front of you. The way you were doing, of calling thread.sleep() between function calls to slow down it's call, was not a good approach. For any timing thing's in swing, use swing timer, i changed your code for swing timer.
Using Swing Timer:
public class exampe extends JPanel implements ActionListener {
int x;
int y;
int temp = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fillOval(x - 2, y - 2, 4, 4);
}
public void set(int X, int Y) {
x = X;
y = Y;
}
public static void main(String args[]) {
JFrame frame = new JFrame("TEST");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exampe ex = new exampe();
JScrollPane scroll = new JScrollPane(ex);
frame.getContentPane().add(scroll);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer PointTimer = new Timer(1000, ex);
PointTimer.setInitialDelay(1000);
PointTimer.start();
System.out.println("started");
}
#Override
public void actionPerformed(ActionEvent e) {
// set(rand.nextInt(350), rand.nextInt(350));
set(temp+10,temp+10);
temp=temp+2;
repaint();
}
}
My aim is to make a paint type application.
Thank you for your suggestions..
but there is still error in the code.
In netbeans IDE error : 'method addActionListener in class cannot be applied to given types' occurs in the line button1.addActionListener(panel);
so i used it suggestion to cast it to ActionListener. so the line is button1.addActionListener((ActionListener) panel);
but still in the below 'if statement' error occurs : cannot find symbol variable 'button1'
Thanks again for your help.
here is changed code. i changed the variable names and used adapter class to make code simpler.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class r extends JPanel
{
public int x1,x2,y1,y2;
public static double SWITCH;
public r()
{
setBackground(Color.WHITE);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
x1=m.getX();
y1=m.getY();
repaint();
}
public void mouseReleased(MouseEvent m)
{
x1=x2=y1=y2=0;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent m)
{
x2=m.getX();
y2=m.getY();
repaint();
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(SWITCH == 2)
{
g.drawRect(x1, y1, x2, y2);
}
else if (SWITCH == 3)
{
g.drawOval(x1,y1,x2,y2);
}
else
{
g.drawString("qwe", x1, y1);
}
}
}
public class q extends JFrame implements ActionListener
{
public static void main(String[] args)
{
q window = new q();
window.setVisible(true);
window.setSize(1024, 800);
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cont = window.getContentPane();
cont.setLayout(new GridLayout(2,2));
r panel = new r();
JPanel BPanel = new JPanel();
cont.add(panel);
cont.add(BPanel);
BPanel.setBackground(Color.blue);
JButton button1,button2;
button1 = new JButton("Rect");
button2 = new JButton("Oval");
BPanel.add(button1);
BPanel.add(button2);
button1.addActionListener((ActionListener) panel);
button2.addActionListener((ActionListener) panel);
}
public void actionPerformed(ActionEvent a)
{
Object obj;
obj=a.getSource();
if (obj== button1)
{
SWITCH = 2;
repaint();
}
else
{
SWITCH = 3;
repaint();
}
}
}
I think your basic Java concepts are lacking.
There is no "this" reference in any static context, i.e. static void main.
Replace "this" with your instance of "r", "z3".
b1.addActionListener(z3);
b2.addActionListener(z3);
Other than that, your UI code is really all over the place, but that's a discussion for another question.