Should I set a background with a central start button. When click on button "start" you must load a "bersaglio" made in another class.
When I run it does not appear the background image but only the start button, I also tried to change the path of the image. Also, when I click on the button shows a "bersaglio".
Where am I wrong?
Home class
public class Home extends JFrame implements ActionListener{
JFrame frame;
JButton b1;
public Home(){
frame= new JFrame();
frame.setSize(200, 200);
frame.setTitle("Bersaglio");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//aggiungo lo sfondo e il bottone
b1=new JButton("START");
Sfondo sfondo=new Sfondo();
JPanel panelsecondo=new JPanel();
panelsecondo.add(b1,BorderLayout.CENTER);
sfondo.add(panelsecondo);
frame.getContentPane().add(sfondo);
b1.addActionListener(this); //aggiungo ascoltatore
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.out.println("entra");
//bersaglio
Bersaglio bersaglio = new Bersaglio();
frame.add(bersaglio);
repaint();
}
}
Sfondo class
class Sfondo extends JPanel{
Image img;
public Sfondo(){
img = Toolkit.getDefaultToolkit().createImage("\\Esdicembre\\EsVacanze\\sfondo");
loadImage(img);
}
private void loadImage(Image img) {
try {
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 0);
mt.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
//setOpaque(false);
g.drawImage(img, 0, 0, null);
super.paintComponent(g);
}
}
class Bersaglio
public class Bersaglio extends JComponent implements MouseListener {
Ellipse2D.Double circle;
Ellipse2D.Double circle1;
Ellipse2D.Double circle2;
Ellipse2D.Double circle3;
/*public void setup(){
x=0;
y=0;
}
/*Color c;
int hight;
int weight;
public Circle() {
super();
}
public Circle(int x,int y,int hight,int weight) {
this.x=x;
this.y=y;
this.hight=hight;
this.weight=weight;
} */
public void paintComponent(Graphics g){
Graphics2D g2;
g2 = (Graphics2D) g;
g2.setColor(Color.BLUE);
circle1 = new Ellipse2D.Double(30, 30, 100, 100);
g2.fill(circle1);
//c1.fillOval(30,30,100,100);
//secondo cerchio medio
g2.setColor(Color.RED);
circle2 = new Ellipse2D.Double(50,50,60,60);
g2.fill(circle2);
//c1.fillOval(50,50,60,60);
//terzo cerchio piccolo
g2.setColor(Color.BLACK);
circle3 = new Ellipse2D.Double(70,70,20,20);
g2.fill(circle3);
//c1.fillOval(70,70,20,20);
addMouseListener(this);
//scrivere qualcosa
//g2.drawString("Ciao", 50, 100);
}
public void mouseClicked (MouseEvent e) {
//x = e.getX();
//y = e.getY();
Point p = e.getPoint();
if(circle3.contains(p)) {
System.out.println("Circle3 contains point");
}else{
if(circle2.contains(p)) {
System.out.println("Circle2 contains point");
}else {
if(circle1.contains(p)) {
System.out.println("Circle1 contains point");
}
}
}
Graphics g = getGraphics();
Graphics2D g2 = (Graphics2D) g;
circle = new Ellipse2D.Double(p.getX(),p.getY(),10,10);
g2.fill(circle);
revalidate();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
Related
This code is kind of a mess. Im trying to prevent the mouse from going outside the red. MouseMotionListener is not working. Ive added The Listener, implemented it. When I try to use e.getX()Nothing shows up! Here is the code: Any help is good!
Class MazeMouse
public class MazeMouse extends JPanel implements ActionListener, MouseMotionListener {
JButton d,k;
JLabel f;
int button = 0;
MazeMouse(){
addMouseMotionListener(this);
setBackground(Color.black);
f= new JLabel(" ");
k = new JButton("Press To Finish");
d = new JButton("Press To Begin");
add(d);
add(f);
add(k);
k.addActionListener(this);
d.addActionListener(this);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(button == 1) {
g2.setColor(Color.red);
//start Rect
g2.fillRect(150,0,200,50);
//below start
g2.fillRect(150, 50, 50, 200);
//finish Rect
g2.fillRect(450, 0, 200, 50);
//below finish
g2.fillRect(600,50,50,200);
//really tiny line
g2.fillRect(150, 250, 500, 1);
}
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==d)
{
button++;
d.setEnabled(false);
repaint();
}
if(e.getSource()==k) {
setBackground(Color.green);
}
}
#Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseMoved(MouseEvent e) {
e.getX();
if(button ==1) {
if(e.getX() < 150 && e.getX() > 350 ) {
System.out.println("Game Over");
}
}
}
}
And MM
public class MM {
JFrame f;
MazeMouse p;
int button = 0;
public MM(){
f = new JFrame();
Container c = f.getContentPane();
c.setLayout(new BorderLayout());
p = new MazeMouse();
c.add(p);
f.setSize(800,800);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[ ]){
MM t = new MM();
}
}
Thanks
Could you please help me to know how to
create a ColorImage object by using the default constructor of the ColorImage class?
create a Canvas object by calling the Canvas constructor with arguments. The first and second arguments of the constructor are the width and the height of the ColorImage. The width and height of a ColorImage have obtain by using the getWidth() and getHeight() methods in the ColorImage class.
public class MyPaint {
public static void main(String[] args) {
new PaintFrame("JavaPainter",100,300);
}
}
class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
private Vector<Point> curve;
private Vector<Vector<Point>> curves;
private Point ptFrom = new Point();
private Point ptTo = new Point();
MyCanvas() {
curve = new Vector<Point>();
curves = new Vector<Vector<Point>>();
this.setPreferredSize(new Dimension(1, 1));
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paintComponent(Graphics g) {
g.setColor(Color.RED);
for (Vector<Point> points : curves) {
Point pt0 = points.get(0);
for (int i = 1; i < points.size(); ++i) {
Point pt = points.get(i);
g.drawLine(pt0.x, pt0.y, pt.x, pt.y);
pt0 = pt;
}
}
}
#Override
public void mousePressed(MouseEvent e) {
ptFrom.x = e.getX();
ptFrom.y = e.getY();
curve.add((Point) ptFrom.clone());
}
#Override
public void mouseReleased(MouseEvent e) {
ptTo.x = e.getX();
ptTo.y = e.getY();
curve.add((Point) ptTo.clone());
curves.add(new Vector<Point>(curve));
curve.clear();
}
#Override
public void mouseDragged(MouseEvent e) {
ptTo.x = e.getX();
ptTo.y = e.getY();
curve.add((Point) ptTo.clone());
Graphics g = getGraphics();
g.setColor(Color.RED);
g.drawLine(ptFrom.x, ptFrom.y, ptTo.x, ptTo.y);
ptFrom.x = ptTo.x;
ptFrom.y = ptTo.y;
}
#Override
public void mouseEntered(MouseEvent e) {
// do nothing
}
#Override
public void mouseExited(MouseEvent e) {
// do nothing
}
#Override
public void mouseClicked(MouseEvent e) {
// do nothing
}
#Override
public void mouseMoved(MouseEvent e) {
// do nothing
}
}
class PaintFrame extends JFrame {
private MyCanvas canvas = new MyCanvas();
PaintFrame(String title) {
super(title);
Container cp = getContentPane();
cp.add(canvas);
setSize(300, 200);
setVisible(true);
}
PaintFrame(String title,Integer length,Integer width) {
super(title);
Container cp = getContentPane();
cp.add(canvas);
setSize(width, length);
setVisible(true);
}
}
From other place http://blog.csdn.net/fduan/article/details/8062556
I'm trying to create a program that able the user to drag and drop the oval around in the space. I was able to drag and drop but after I tried do it again on the second run, the oval jump all over the places. I was wondering if anyone know why this happen? Am i missing something? Thank you
public class MoveOval extends JFrame {
private Ellipse2D node = new Ellipse2D.Float(200,200,80,120);
private Point offset;
private int preX,preY;
private Image dbImage;
private Graphics dbg;
Adapter ma = new Adapter();
public static void main(String args[]){
JFrame frame = new MoveOval();
frame.setSize(600,600);
frame.setVisible(true);
}
public MoveOval(){
super("Move Oval");
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(ma);
addMouseMotionListener(ma);
}
private class Adapter extends MouseAdapter{
public void mousePressed(MouseEvent e){
if(node.contains(e.getPoint())){
preX = node.getBounds().x-e.getX();
preY = node.getBounds().y-e.getX();
offset = new Point(preX, preY);
}
}
public void mouseDragged(MouseEvent e){
if(node.contains(e.getPoint())){
updateLocation(e);
}
}
public void mouseReleased(MouseEvent e) {
offset=null;
}
}
public void updateLocation(MouseEvent e){
Point to = e.getPoint();
to.x += offset.x;
to.y += offset.y;
Rectangle bounds = node.getBounds();
bounds.setLocation(to);
node.setFrame(bounds);
repaint();
}
public void paint(Graphics g){
dbImage=createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g){
Graphics2D gd = (Graphics2D)g.create();
gd.setColor(Color.blue);
gd.fill(node);
}
}
Actually a very simple mistake and easy to fix.
public void mousePressed(MouseEvent e){
if(node.contains(e.getPoint())){
preX = node.getBounds().x-e.getX();
preY = node.getBounds().y-e.getX(); // <- That's the bad guy.
offset = new Point(preX, preY);
}
}
It has to be -e.getY() not -e.getX().
I'm kinda new to java and have been trying to make a simple paint program, I have gotten everything to work except the color of the paint brush. Rigth now I set the color to blue but I want to make the color of the paint brush the same color as the color selected by the color slider.
Here's the code I got so far
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Paint extends JFrame implements ChangeListener{
PaintPanel color;
PaintPanel2 paint;
JSlider red;
JSlider green;
JSlider blue;
public Paint(){
super("Paint");
setSize(300,290);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
color = new PaintPanel();
paint = new PaintPanel2();
red = new JSlider(0,255,255);
green = new JSlider(0,255,0);
blue = new JSlider(0,255,0);
red.setMajorTickSpacing(50);
red.setMinorTickSpacing(10);
red.setPaintTicks(true);
red.setPaintLabels(true);
red.addChangeListener(this);
green.setMajorTickSpacing(50);
green.setMinorTickSpacing(10);
green.setPaintTicks(true);
green.setPaintLabels(true);
green.addChangeListener(this);
blue.setMajorTickSpacing(50);
blue.setMinorTickSpacing(10);
blue.setPaintTicks(true);
blue.setPaintLabels(true);
blue.addChangeListener(this);
JLabel redLabel = new JLabel("Red: ");
JLabel greenLabel = new JLabel("Green: ");
JLabel blueLabel = new JLabel("Blue: ");
GridLayout grid = new GridLayout(5,1);
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
setLayout(grid);
JPanel redPanel = new JPanel();
redPanel.setLayout(flow);
redPanel.add(redLabel);
redPanel.add(red);
add(redPanel);
JPanel greenPanel = new JPanel();
greenPanel.setLayout(flow);
greenPanel.add(greenLabel);
greenPanel.add(green);
add(greenPanel);
JPanel bluePanel = new JPanel();
bluePanel.setLayout(flow);
bluePanel.add(blueLabel);
bluePanel.add(blue);
add(bluePanel);
add(color);
add(paint);
setVisible(true);
}
public void stateChanged(ChangeEvent e){
JSlider source = (JSlider) e.getSource();
if(source.getValueIsAdjusting() != true){
Color mainColor = new Color(red.getValue(),
green.getValue(),
blue.getValue());
color.changeColor(mainColor);
color.repaint();
}
}
public static void main(String[] args){
Paint p = new Paint();
}
}
class PaintPanel extends JPanel{
Color background;
public PaintPanel(){
background = Color.red;
}
public void paintComponent(Graphics comp){
Graphics2D comp2D = (Graphics2D) comp;
comp2D.setColor(background);
comp2D.fillRect(0,0,getSize().width,getSize().height);
}
void changeColor(Color newBackground){
background = newBackground;
}
}
class PaintPanel2 extends JPanel{
Image image;
Graphics2D comp2D;
int currentX, currentY, oldX, oldY;
PaintPanel color;
public PaintPanel2(){
color = new PaintPanel();
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
currentX = e.getX();
currentY = e.getY();
if(comp2D != null)
comp2D.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
});
}
public void paintComponent(Graphics comp){
if(image == null){
image = createImage(getSize().width, getSize().height);
comp2D = (Graphics2D)image.getGraphics();
comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
comp2D.setPaint(Color.white);
comp2D.fillRect(0, 0, getSize().width, getSize().height);
comp2D.setPaint(Color.blue);
repaint();
}
comp.drawImage(image, 0, 0, null);
}
}
The problem was that you weren't setting the chosen color in PaintPanel2. I changed the stateChanged method and the PaintPanel2 as follows, and now it works as I assume you intended:
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
if (source.getValueIsAdjusting() != true) {
Color mainColor = new Color(red.getValue(),
green.getValue(),
blue.getValue());
color.changeColor(mainColor);
paint.setPaintColor(mainColor);
color.repaint();
}
}
class PaintPanel2 extends JPanel {
Image image;
Graphics2D comp2D;
int currentX, currentY, oldX, oldY;
public PaintPanel2() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
if (comp2D != null) {
comp2D.drawLine(oldX, oldY, currentX, currentY);
}
repaint();
oldX = currentX;
oldY = currentY;
}
});
}
public void paintComponent(Graphics comp) {
if (image == null) {
image = createImage(getSize().width, getSize().height);
comp2D = (Graphics2D) image.getGraphics();
comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
comp2D.setPaint(Color.white);
comp2D.fillRect(0, 0, getSize().width, getSize().height);
comp2D.setPaint(Color.blue);
repaint();
}
comp.drawImage(image, 0, 0, null);
}
public void setPaintColor(Color color) {
comp2D.setColor(color);
}
}
package test;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class test_bmp extends JPanel implements MouseListener,MouseMotionListener,ActionListener
{
static BufferedImage image;
Color color;
Point start=new Point();
Point end =new Point();
JButton elipse=new JButton("Elipse");
JButton rectangle=new JButton("Rectangle");
JButton line=new JButton("Line");
String selected;
public test_bmp()
{
color = Color.black;
setBorder(BorderFactory.createLineBorder(Color.black));
addMouseListener(this);
addMouseMotionListener(this);
}
public void paintComponent(Graphics g)
{
//super.paintComponent(g);
g.drawImage(image, 0, 0, this);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.black);
if(selected=="elipse")
{
g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y));
System.out.println("Start : "+start.x+","+start.y);
System.out.println("End : "+end.x+","+end.y);
}
if(selected=="line")
g2.drawLine(start.x,start.y,end.x,end.y);
}
//Draw on Buffered image
public void draw()
{
Graphics2D g2 = image.createGraphics();
g2.setPaint(color);
System.out.println("draw");
if(selected=="line")
g2.drawLine(start.x, start.y, end.x, end.y);
if(selected=="elipse")
{
g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y));
System.out.println("Start : "+start.x+","+start.y);
System.out.println("End : "+end.x+","+end.y);
}
repaint();
g2.dispose();
}
public JPanel addButtons()
{
JPanel buttonpanel=new JPanel();
buttonpanel.setBackground(color.lightGray);
buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS));
elipse.addActionListener(this);
rectangle.addActionListener(this);
line.addActionListener(this);
buttonpanel.add(elipse);
buttonpanel.add(Box.createRigidArea(new Dimension(15,15)));
buttonpanel.add(rectangle);
buttonpanel.add(Box.createRigidArea(new Dimension(15,15)));
buttonpanel.add(line);
return buttonpanel;
}
public static void main(String args[])
{
test_bmp application=new test_bmp();
//Main window
JFrame frame=new JFrame("Whiteboard");
frame.setLayout(new BorderLayout());
frame.add(application.addButtons(),BorderLayout.WEST);
frame.add(application);
//size of the window
frame.setSize(600,400);
frame.setLocation(0,0);
frame.setVisible(true);
int w = frame.getWidth();
int h = frame.getHeight();
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setPaint(Color.white);
g2.fillRect(0,0,w,h);
g2.dispose();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
#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 event)
{
start = event.getPoint();
}
#Override
public void mouseReleased(MouseEvent event)
{
end = event.getPoint();
draw();
}
#Override
public void mouseDragged(MouseEvent e)
{
end=e.getPoint();
repaint();
}
#Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==elipse)
selected="elipse";
if(e.getSource()==line)
selected="line";
draw();
}
}
I need to create a paint application. When I draw ellipse by dragging mouse from left to right it displays nothing. Why? Should I use any other function here?
Your program does draw an ellipse when you drag the mouse down and to the right. It's dragging up and/or left that does not work, because Graphics.drawOval does not work with a negative width or height.
Try adding a method like this:
private Shape createEllipse() {
Ellipse2D e = new Ellipse2D.Double();
e.setFrameFromDiagonal(start, end);
return e;
}
Then call it from draw and paintComponent like this:
if(selected=="elipse") {
g2.draw(createEllipse());
}
Also you probably do not need the call to draw() at the end of actionPerformed. If you switch between line and ellipse mode it will draw an ellipse with the same coordinates as the most recent line or vice-versa.
And one coding style issue: Using string literals for selected is confusing (although it does work.) I would define an enum instead.