This week's book giveaway is in the
Agile and other Processes
forum.
We're giving away four copies of
The Mikado Method
and have Ola Ellnestam and Daniel Brolund on-line!
See
this thread
for details.
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Applets
Author
How to add in button and paint in applet?
Edward Sie
Greenhorn
Joined: Mar 08, 2011
Posts: 5
posted
Mar 12, 2011 01:35:54
0
i having problem of adding the button and paint graphic together.. should it using paint Component?
import java.applet.*; import java.awt.*; import java.util.*; import java.net.*; import java.net.URL; import java.io.*; import javax.imageio.*; import javax.swing.*; public class Player implements Runnable{ private String name; private int age; private int x =0, y=0, speed, z; //Thread th; public Player(String name, int age, int x, int y, int speed){ this.name = name; this.age = age; this.x = x; this.y = y; this.speed = speed; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } 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 start(){ } public void run(){ try{ while(true) { x++; Thread.sleep(speed); //if(x == 535){ //break; // } } } catch(Exception e) { System.out.println(e); } } }
import java.awt.*; import javax.swing.*; import java.net.URL; import java.io.*; import javax.imageio.*; import java.util.concurrent.*; import java.util.*; import java.applet.*; public class RaceAnimate extends JApplet implements Runnable{ Thread th0, th1,th2,th3; Graphics bufferGraphics; Image car1, car2, car3, offscreen, background; private JButton jbtStart = new JButton("Start"); static Player player1 = new Player("Edward", 19, 0, 130, 15); static Player player2 = new Player("Johnson", 21, 0, 260, 8); static Player player3 = new Player("Bernard", 21, 0, 400, 10); public void init(){ setLayout(new FlowLayout()); add(jbtStart); offscreen = createImage(getSize().width, getSize().height); bufferGraphics = offscreen.getGraphics(); System.out.println("swt"); car1=getImage(getCodeBase(),"images/car1(new).png"); car2=getImage(getCodeBase(),"images/car2(new).png"); car3=getImage(getCodeBase(),"images/car3(new).png"); background=getImage(getCodeBase(),"images/road2.jpg"); } public void paint(Graphics g){ th0 = new Thread(this); th0.start(); bufferGraphics.drawImage(background,0,0,this); bufferGraphics.drawImage(car1,player1.getX(),player1.getY(),this); bufferGraphics.drawImage(car2,player2.getX(), player2.getY(),this); bufferGraphics.drawImage(car3,player3.getX(),player3.getY(),this); g.drawImage(offscreen,0,0,this); } public void start(){ th1 = new Thread(player1); th1.start(); th2 = new Thread(player2); th2.start(); th3 = new Thread(player3); th3.start(); } public void update(Graphics g) { paint(g); } public void run(){ try{ repaint(); } catch(Exception e) { System.out.println(e); } if(player1.getX() == 535){ th1.stop(); } if(player2.getX() == 535){ th2.stop(); } if(player3.getX() == 535){ th3.stop(); } } public static void main(String[] args){ JFrame frame = new JFrame("Practical 1"); RaceAnimate applet = new RaceAnimate(); frame.add(applet); applet.init(); applet.start(); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,400); frame.setVisible(true); } }
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35253
7
posted
Mar 12, 2011 01:57:28
0
should it using paintComponent?
Yes. See
Lesson: Performing Custom Painting
and
Painting in AWT and Swing
.
Android apps
–
ImageJ plugins
–
Java web charts
Edward Sie
Greenhorn
Joined: Mar 08, 2011
Posts: 5
posted
Mar 12, 2011 23:21:03
0
yeap~ but i cnt manage to add in paintcomponent.. actually i dono how to use paint component~ i hope can be teach =)
I agree. Here's the link:
http://aspose.com/file-tools
subject: How to add in button and paint in applet?
Similar Threads
JTextFields & storing data
class variables exp w/?
the display of my applet in html got problem~
Racing game does not work(HELP!!!!)
how to access instance variables
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter