I am trying to create a canvas in which I can paint,and a button which resets the canvas in an empty state.
However using BorderLayout my button gets duplicated,the second one is an image copy of the first one but still it doesnt look good.
Is there anything wrong in my code and how can it be fixed.
PaintCanvas.java
import javax.swing.* ;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
class framakryesore extends JFrame {
PaintCanvas p1 = new PaintCanvas();
JButton b1 = new JButton ("Reset");
public framakryesore (){
b1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
clearcanvas(p1);
repaint();
}
});
this.add(b1,BorderLayout.NORTH);
this.add(p1,BorderLayout.CENTER);
}
public void clearcanvas(PaintCanvas p){
p.setX(0);
p.setY(0);
}
}
public class PaintCanvas extends JPanel {
private int x = 0 ;
private int y = 0 ;
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public PaintCanvas() {
addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e){
x=e.getX();
y=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e){
}
});
}
#Override
protected void paintComponent (Graphics g){
if (x==0 && y==0){
super.paintComponent(g);
}
else
{
g.setFont(new Font("TimesRoman", Font.BOLD, 30));
g.drawString(".", x, y);
}
}
}
PaintCanvasTest.java
import javax.swing.JFrame;
public class PaintCanvasTest {
public static void main(String args[]){
framakryesore pikturo = new framakryesore();
pikturo.setVisible(true);
pikturo.setSize(640, 480);
pikturo.setTitle("Pikturo");
pikturo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
if (x==0 && y==0){
super.paintComponent(g);
You should always invoke super.paintComponent(). It is responsible for clearing the background of the panel before you do your custom painting.
Related
Here I have two classes, one including main function. I want to draw a rectangle which moves automatically. But the starting point of rectangle is not the same as the point i clicked with mouse. I could not figure out this problem. Can you help me?
This is the first class including main function
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class BuyuyenSuDamlalari extends JPanel implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600,400);
BuyuyenSuDamlalari bsd1 = new BuyuyenSuDamlalari();
frame.setContentPane(bsd1);
BuyuyenSuDamlalariClickListener bscl = new BuyuyenSuDamlalariClickListener(bsd1);
bsd1.addMouseListener(bscl);
frame.setResizable(false);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
int x;
int y;
int radius;
int click;
public BuyuyenSuDamlalari() {
super();
setFocusable(true);
Timer zaman = new Timer(40, this); // bir saniyede 25 resim oluyor
zaman.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (click>0) {
g.drawRect(x, y, radius, radius);
}
}
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 actionPerformed(ActionEvent arg0) {
radius++;
repaint();
}
public void setClick(int click) {
this.click = click;
}
}
This is the second class wihich includes motionListener
import java.awt.event.*;
public class BuyuyenSuDamlalariClickListener extends MouseAdapter {
private BuyuyenSuDamlalari bsd = new BuyuyenSuDamlalari();
private int x;
private int y;
public BuyuyenSuDamlalariClickListener(BuyuyenSuDamlalari bsd) {
super();
this.bsd = bsd;
}
public void mousePressed(MouseEvent e) {
if (e.getClickCount()>0) {
bsd.setX(e.getX()-25);
bsd.setY(e.getY()-25);
}
bsd.setClick(1);
}
}
I am trying to draw a line at run time on any one layer of JlayeredPane. What i am currently facing is that line drawn getting erased automatically once i release the mouse. I want that drawn line to be there until i click the mouse again.
I am calling the below written class, this way
iDimension = new getDimension();
iDimension.setBounds(1, 12, 441, 380);
//iDimension.setOpaque(true);
iDimension.setBackground(new Color(0,0,0,100));
I have added iDimension With Layered pane in this way
layeredPane.add(iDimension, new Integer(1),0);
Here is the getDimension Class
public class getDimension extends JPanel {
public getDimension() {
setDoubleBuffered(true);
this.setBorder(UIManager.getBorder("ComboBox.border"));
this.repaint();
}
Point pointStart = null;
Point pointEnd = null;
{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
pointEnd = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
repaint();
}
});
}
public void paint(Graphics g) {
super.paint(g);
if (pointStart != null) {
g.setColor(Color.GREEN);
g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
// System.out.println("" + pointStart.x +"," + pointStart.y +"," + pointEnd.x +"," +pointEnd.y);
}
}
}
I am a newbie in java. Kindly correct if there is any ambiguity in my question.
What i am currently facing is that line drawn getting erased automatically once i release the mouse. I want that drawn line to be there until i click the mouse again.
The code is only doing what you tell it to do:
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
});
and:
public void paint(Graphics g) {
super.paint(g);
if (pointStart != null) { // *********
g.setColor(Color.GREEN);
g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
}
}
Note that if pointStart is null, you don't draw the line -- but you set it to null on mouseReleased! Solution -- don't do that.
e.g.,
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
#SuppressWarnings("serial")
public class GetDimension extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private Point pointStart = null;
private Point pointEnd = null;
public GetDimension() {
this.setBorder(UIManager.getBorder("ComboBox.border"));
this.repaint();
MouseAdapter myMouse = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
repaint();
}
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
repaint();
}
};
addMouseListener(myMouse);
addMouseMotionListener(myMouse);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (pointStart != null && pointEnd != null) {
g.setColor(Color.GREEN);
g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
}
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
GetDimension mainPanel = new GetDimension();
JFrame frame = new JFrame("GetDimension");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}
I can draw static things to the screen, but I want to make them move with user key input. I don't know what to do, I've been searching and searching and haven't come up with an answer yet. Please help!
package com.Game.game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JFrame
{
final static int width = 500;
final static int height = 500;
public int x = 250;
public int y = 250;
public int changeX = 10;
public int changeY = 10;
public static void main(String[] args)
{
new Game();
}
public Game()
{
KeyListener listener = new KeyListening();
addKeyListener(listener);
setFocusable(true);
DrawingStuff drawingstuff = new DrawingStuff();
add(drawingstuff);
setSize(width, height);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public class DrawingStuff extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("Hey there!", 300, 300);
g.setColor(Color.RED);
g.fillRect(x, y, 50, 50);
}
}
public class KeyListening implements KeyListener
{
DrawingStuff drawingstuff = new DrawingStuff();
#Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_UP)
{
y = y + changeY;
System.out.println("Hey");
drawingstuff.repaint();
}
}
#Override
public void keyReleased(KeyEvent e)
{
}
#Override
public void keyTyped(KeyEvent e)
{
}
}
public void update()
{
}
}
EDIT: Fixed it. I took away the key listener stuff in the constructor method, added a command to focus on "drawingstuff" in the constructor method, and, most importantly, added this bit of code to the end of the constructor method:
while(true)
{
drawingstuff.repaint();
}
The problem is that your KeyListening object has a reference to a different DrawingStuff object than the one you added to your UI inside the Game constructor.
public class KeyListening implements KeyListener
{
DrawingStuff drawingstuff = new DrawingStuff();
...
You should pass a DrawingStuff reference to the KeyListening instance so that it can tell the right object to repaint itself.
I got a program running and I call this class and well, after coding it it should do stuff when the mouse drags, but it just does nothing, where is the error? I mean it should work based on the code, but it just stares at me with apparent ambivalent contempt, mocking me.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class simpleWhiteBoard extends JFrame
{
protected int lastX, lastY;
public simpleWhiteBoard()
{
super("Smile white board");
lastX = 0;
lastY = 0;
Container c = getContentPane();
c.setBackground(Color.white);
addMouseListener(new positionRecorder());
addMouseMotionListener(new LineDrawer());
setBounds(10, 20, 450, 400);
setVisible(true);
}
protected void record(int x, int y)
{
lastX = x;
lastY = y;
}
private class positionRecorder extends MouseAdapter
{
public void mouseEntered(MouseEvent e)
{
record(e.getX(), e.getY());
}
public void mousePressed(MouseEvent e)
{
record(e.getX(), e.getY());
}
}
private class LineDrawer extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
Graphics g = getGraphics();
Polygon p = new Polygon(Constants.X, Constants.Y, Constants.X.length);
g.setColor(Color.yellow);
g.fillPolygon(p);
g.setColor(Color.red);
p = new Polygon(Constants.X, Constants.Y, Constants.X.length);
}
}
}//finale
I am new to Java.
I want to create a program, in which i need to draw freely on JPanel by dragging the mouse. I go through the basics of paint function and able to achieve this.
public class DrawLine extends JPanel {
public void paint(Graphics g)
{
g.drawLine(0, 0, 50, 50);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
JFrame frame=new JFrame("Top Level Demo");
frame.setSize(300, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel myPanel=new JPanel();
myPanel.setLayout(null);
frame.add(myPanel);
frame.add(new DrawLine());
frame.setVisible(true);
}});
}}
But this generates an out put like this where the straight line is determined by coordinates.
Please some one help me to implement free drawing inside JPanel.
Override paintComponent() rather than paint() method of JPanel.
Create list of Point to be used in the paintComponent().
Iterate the list in a loop and For each pair of points from the list call
g.drawLine(currentPoint.x,currentPoint.y, nextPoint.x,nextPoint.y);
Add drag listening to store drag points in the list.
Try to use MouseListener and paintComponent method.
Try next simple example for drawing your line.
public class Test extends JPanel{
public static int xS = 0;
public static int yS = 0;
public static int xF = 0;
public static int yF = 0;
public static void main(String[] args){
JFrame frame = new JFrame("Movement of 2d Shapes");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Test t = new Test();
t.setOpaque(true);
t.addMouseListener(getMouseListener(t));
frame.getContentPane().add(t);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static MouseListener getMouseListener(final Test t) {
return new MouseAdapter() {
#Override
public void mousePressed(MouseEvent arg0) {
xS = arg0.getPoint().x;
yS = arg0.getPoint().y;
}
#Override
public void mouseReleased(MouseEvent arg0) {
xF = arg0.getPoint().x;
yF = arg0.getPoint().y;
t.repaint();
}
};
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(xS,yS, xF, yF);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawLine extends JFrame implements MouseMotionListener {
int x1,y1,x,y;
private boolean first = true;
public DrawLine() {
super("Top Level Demo");
setSize(300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBackground(Color.white);
addMouseMotionListener(this);
}
#Override
public void mouseDragged(MouseEvent e) {
x =x1; y = y1;
x1 = e.getX();
y1 = e.getY();
if(first){
x = x1;
y = y1;
first = false;
}
repaint();
}
#Override
public void mouseMoved(MouseEvent e) {
}
public void paint(Graphics graphics){
graphics.drawLine(x, y, x1, y1);
}
public static void main(String[] args) {
new DrawLine();
}
}